在我的项目中,我创建了一个透明且无框架的QMainWindow,然后创建了QmlApplicationViewer。我需要能够拖动窗口并调整其大小。 我该怎么办?
答案 0 :(得分:3)
此应用程序是此处提供的一个小变体,用于处理transparent windows in QML applications:
<强> win.cpp:强>
#include <QApplication>
#include <QDeclarativeView>
#include <QMainWindow>
#include <QDeclarativeContext>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
QDeclarativeView* v = new QDeclarativeView;
window.setCentralWidget(v);
v->setSource(QUrl::fromLocalFile(("draw_rectangles.qml")));
// expose window object to QML
v->rootContext()->setContextProperty("mainwindow",&window);
window.setStyleSheet("background:transparent;");
window.setAttribute(Qt::WA_TranslucentBackground);
window.setWindowFlags(Qt::FramelessWindowHint);
window.show();
app.exec();
}
<强> win.pro:强>
TEMPLATE += app
QT += gui declarative
SOURCES += win.cpp
<强> draw_rectangles.qml:强>
import Qt 4.7
Item {
Rectangle {
opacity: 0.5
color: "red"
width: 100; height: 100
MouseArea {
anchors.fill: parent
onPressed: {
mainwindow.size.width = 200;
mainwindow.size.height = 500;
}
}
Rectangle {
color: "blue"
x: 50; y: 50; width: 100; height: 100
MouseArea {
id: mouseRegion
anchors.fill: parent;
property variant clickPos: "1,1"
onPressed: {
clickPos = Qt.point(mouse.x,mouse.y)
}
onPositionChanged: {
var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
mainwindow.pos = Qt.point(mainwindow.pos.x+delta.x,
mainwindow.pos.y+delta.y)
}
}
}
}
}
即使您对透明度不感兴趣,此应用程序也会展示如何将QMainWindow
公开给QML。这允许QML应用程序在主窗口中进行更改。
单击蓝色矩形以拖动窗口,然后使用qml中的硬编码值单击红色矩形以调整窗口大小。当然,由于窗口是透明的,因此在调整窗口大小时,您将无法获得常规不透明应用程序的视觉反馈。但调整大小的操作是有效的。享受!