代码之家  ›  专栏  ›  技术社区  ›  jpo38

为什么要获取QWindowsWindow::setGeometry:无法使用Qt5.12.0设置几何警告

  •  4
  • jpo38  · 技术社区  · 6 年前

    我将一些代码从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).
    

    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();
    }
    

    主机.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和Windows 10下看到了这个问题。我做错什么了吗?

    setMinimumSize (见 https://stackoverflow.com/a/31231069/3336423

    1 回复  |  直到 6 年前
        1
  •  3
  •   cbuchart    6 年前

    正如您所提到的,这个问题只发生在Windows中: QWindowsWindow 类是 windows 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

    Dialog with help & close buttons

    例如,如果删除“帮助”按钮( dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowContextHelpButtonHint);

    Dialog without help button