QDockWidget sized wrong when docked on right side of Main Window

折月煮酒 提交于 2019-12-12 13:01:40

问题


I am new to Qt and I am trying to create a DockWidget that docks on the right of the window. I set a maximum and minimum width for the dock (as you will see in the code below). This works if the dock widget is added with Qt::LeftDockWidgetArea, but when it is added with Qt::RightDockWidgetArea, The dock is "padded" out to the center of the window, like this:

I am probably not sizing the dock in the correct way.. Here is the code for this window:

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

Is there a "correct" way to do this?


回答1:


As stated in the documentation:

Note: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.




回答2:


Yes! You can't creating a main window without a central widget, But you can set central widget's height to zero. MainWindow.cpp

centralWidget()->setMaximumHeight(0);   


来源:https://stackoverflow.com/questions/10067821/qdockwidget-sized-wrong-when-docked-on-right-side-of-main-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!