为什么我得到QWindowsWindow :: setGeometry:无法使用Qt 5.12.0设置几何警告

jpo*_*o38 5 c++ qt

我将一些代码从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).
Run Code Online (Sandbox Code Playgroud)

main.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();
}
Run Code Online (Sandbox Code Playgroud)

mainframe.h:

#include <QMainWindow>

class QPushButton;
class MainFrame : public QMainWindow
{
    Q_OBJECT

public:
    MainFrame();

public slots:
    void showPopup();

private:
    QPushButton* button;
};
Run Code Online (Sandbox Code Playgroud)

mainframe.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();
}
Run Code Online (Sandbox Code Playgroud)

我在Windows 7和10下看到了该问题。我做错了吗?

我知道可以通过设置删除警告setMinimumSize(请参阅/sf/answers/2186174861/),但是为什么我们应该对创建的每个小部件都这样做呢?有没有办法永久解决这个问题?

cbu*_*art 5

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

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

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);
  // ...
}
Run Code Online (Sandbox Code Playgroud)

更新

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

带有帮助和关闭按钮的对话框

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

没有帮助按钮的对话框


jpo*_*o38 5

该问题已报告给 Qt: https ://bugreports.qt.io/browse/QTBUG-73258

对于OP中的代码来说是好的,这只是一个Qt错误。

它被标记为“P2重要”,所以希望它应该在下一个版本中修复。


更新:Qt 6.2.2 中仍未修复...