我的任务是为QtQuick创建一个窗口,它可以像普通窗口一样使用,但具有自定义框架外观(不是默认的系统装饰)。我希望达到类似于Visual窗口的效果,或者类似这样的效果。
允许我实现该目标的代码如下所示:
main.cpp
#include <QtQuick/qquickpainteditem.h>
#include <qapplication.h>
#include <qqmlengine.h>
#include <QtQuick/qquickwindow.h>
class frameLess :
public QQuickWindow
{
public:
frameLess(QWindow *parent = 0) :QQuickWindow(parent) { }
bool nativeEvent(const QByteArray& eventType, void* message, long* result)
{
MSG *msg = static_cast<MSG *>(message);
switch (msg->message)
{
case WM_SHOWWINDOW:
// after that call Qt considers window as frameless
setFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
// that call force the Windows to serve users mouse events like in standard window
SetWindowLongPtr(msg->hwnd, GWL_STYLE, WS_POPUP | WS_CAPTION | WS_THICKFRAME | WS_MAXIMIZEBOX | WS_MINIMIZEBOX);
return false;
case WM_NCCALCSIZE:
// prevent the Windows from painting the frame
*result = 0;
return true;
default:
break;
}
return false;
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<frameLess>("fb", 1, 0, "FrameLess");
QQmlEngine engine;
QQmlComponent *component = new QQmlComponent(&engine);
QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
component->loadUrl(QUrl("qrc:////main.qml"));
if (!component->isReady())
{
qWarning("%s", qPrintable(component->errorString()));
return -1;
}
QObject *topLevel = component->create();
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
QSurfaceFormat surfaceFormat = window->requestedFormat();
window->setFormat(surfaceFormat);
window->show();
return app.exec();
}main.qml
import fb 1.0
import QtQuick 2.2
import QtQuick.Controls 1.1
FrameLess {
color:"lightgray"
Rectangle {
width: 45
height: 45
color: "green"
anchors {
top: parent.top
left: parent.left
}
MouseArea {
anchors.fill: parent
hoverEnabled: true;
onEntered: parent.color="red"
onExited: parent.color="green"
}
}
}因此,左上角应该出现绿色矩形的无框窗口。此外,当鼠标悬停时,该矩形应改变颜色为红色。
当我用基于角度的Qt构建时,一切都像预期的那样工作。

然而,我的团队正在使用基于OpenGL的Qt构建.问题是,当我的代码与Qt的这种版本相链接时,绘制区域会根据窗口框架的大小而移动:

更重要的是,只有绘画领域被转移。例如,鼠标命中器在适当的位置。正因为如此,命中盒和场景项坐标被去同步。( MouseArea的交互区域位于与其填充的矩形不同的位置)

我的期望是,使用不同的Qt构建不应影响最终的应用程序。
我的问题是,为什么使用基于OpenGL的Qt构建确实会影响窗口外观,以及如何实现可移植性(跨windows Qt构建)预期的行为。
我使用的构建是:
我正在使用Windows8.1进行测试
更新:似乎在QT5.4.0中修复了这个错误
发布于 2014-07-28 17:31:40
这是个Qt错误。请如实报告。您不需要在代码中做任何特殊的事情才能正常运行。
https://stackoverflow.com/questions/24994033
复制相似问题