当在主窗口的右侧停靠时,QDockWidget 大小错误

3
我刚开始学习Qt,尝试创建一个停靠在窗口右侧的DockWidget。我为Dock设置了最大和最小宽度(如下面的代码所示)。如果使用Qt::LeftDockWidgetArea添加DockWidget,它可以正常工作。但是,当使用Qt::RightDockWidgetArea添加时,Dock会被“填充”到窗口的中心位置,如下图所示: The red area shows the dock widget boundary.

我可能没有正确设置Dock的大小... 下面是这个窗口的代码:

int main(int argv, char** args)
{
    QApplication app(argv, args);
    QMainWindow window;
    QDesktopWidget* desktop = QApplication::desktop();
    //Docks
    QDockWidget* propertyDock = new QDockWidget("",&window);
    QWidget* propertyDockContents = new QWidget;

    //This sets the window in the center of the screen.
    int wWidth = 800; int wHeight = 600;
    window.setGeometry(QRect( (desktop->width()-wWidth)/2 , (desktop->height()-wHeight)/2 ,wWidth,wHeight));

    propertyDock->setAllowedAreas(Qt::RightDockWidgetArea);
    propertyDockContents->setMaximumWidth(200);
    propertyDockContents->setMinimumWidth(20);

    propertyDock->setWidget(propertyDockContents);
    window.addDockWidget(Qt::RightDockWidgetArea,propertyDock);

    window.show();

    return app.exec();
}

有没有一种“正确”的方法来做这件事?
2个回答

3

如文档所述:

注意:不支持创建没有中央窗口部件的主窗口。即使只是一个占位符,您也必须拥有一个中央窗口部件。


0

可以的!你不能在没有中心控件的情况下创建主窗口,但是你可以将中心控件的高度设置为零。 MainWindow.cpp

centralWidget()->setMaximumHeight(0);   

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接