gpt4 book ai didi

c++ - 为什么我会收到 QWindowsWindow::setGeometry: Unable to set geometry warning with Qt 5.12.0

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:30 28 4
gpt4 key购买 nike

我将一些代码从 Qt 5.6.0 迁移到了 5.12.0。令人惊讶的是,我收到了很多与 QWindowsWindow::setGeometry 相关的警告。每当一个对话框显示在另一个对话框之上时,我都会收到此警告。

我可以在 MCVE 中隔离问题,它非常简单和最小,所有父子关系看起来都不错,但是,当按下按钮时我们会收到警告:

QWindowsWindow::setGeometry: Unable to set geometry 132x30+682+303 on QWidgetWindow/'QDialogClassWindow'. Resulting geometry:  132x42+682+303 (frame: 4, 28, 4, 4, custom margin: 0, 0, 0, 0, minimum size: 116x42, maximum size: 16777215x16777215).

主要.cpp:

#include <QApplication>
#include "mainframe.h"
#include <qDebug>

void MessageOutput( QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
qDebug() << msg;
}

int main( int argc, char* argv[] )
{
QApplication app(argc, argv);

qInstallMessageHandler(MessageOutput);

MainFrame wnd;
wnd.show();

return app.exec();
}

大型机.h:

#include <QMainWindow>

class QPushButton;
class MainFrame : public QMainWindow
{
Q_OBJECT

public:
MainFrame();

public slots:
void showPopup();

private:
QPushButton* button;
};

大型机.cpp:

#include "mainframe.h"

#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>

MainFrame::MainFrame()
{
QWidget* widget = new QWidget( this );
widget->setLayout( new QVBoxLayout( widget ) );

QPushButton* pTextButton = new QPushButton( "Show popup", widget );
widget->layout()->addWidget( pTextButton );
connect( pTextButton, SIGNAL(clicked()), this, SLOT(showPopup()) );

setCentralWidget( widget );
}

void MainFrame::showPopup()
{
QDialog dlg( this );
dlg.setLayout( new QVBoxLayout() );
dlg.layout()->addWidget( new QLabel("popup message",&dlg) );
dlg.exec();
}

我在 Windows 7 和 10 下看到了这个问题。我做错了什么吗?

我知道可以通过设置 setMinimumSize 来移除警告(参见 https://stackoverflow.com/a/31231069/3336423 ),但是为什么我们要为我们创建的每个小部件都这样做呢?有办法永久解决这个问题吗?

最佳答案

正如您所提到的,此问题仅发生在 Windows 中:QWindowsWindow 类是 windows 平台插件的一部分。查看 Qt 的源代码 (qwindowswindow.cpp@QWindowsWindow::setGeometry) 没有直接的方法来暂停该特定消息。

我现在能想到的唯一全局解决方案是使用 message handler 过滤警告消息:

void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
if (type != QtWarningMsg || !msg.startsWith("QWindowsWindow::setGeometry")) {
QByteArray localMsg = msg.toLocal8Bit();
fprintf(stdout, localMsg.constData());
}
}

int main(int argc, char* argv[])
{
qInstallMessageHandler(myMessageOutput);
QApplication a(argc, argv);
// ...
}

更新

问题之一是 Windows 将自己的按钮添加到框架中。在您的示例中,对话框添加了三个按钮:系统按钮(图标,左上角)、帮助按钮和关闭按钮。帮助和关闭按钮具有固定大小,恰好大于 QDialog 的框架(计算为请求大小和 minimumSize 之间的最大值)。然后会生成警告:您请求的大小与 Windows 创建的大小不匹配:

Dialog with help & close buttons

如果您删除帮助按钮,例如 (dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowContextHelpButtonHint);),则警告会消失,而无需为窗口设置最小尺寸.必须对显示的每个对话框执行手动操作,但我认为自动化比最小尺寸更容易(也许通过工厂?):

Dialog without help button

关于c++ - 为什么我会收到 QWindowsWindow::setGeometry: Unable to set geometry warning with Qt 5.12.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54307407/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com