Important: Please read the Qt Code of Conduct -

Mainwindow layout problem with QDockWidget.

  • Hi Friends,

    I have to create a custom layout for my main application. Layout can be of different type as:

    | | | | |
    | 1 | | 4 | 6 |
    |-----| 3 |-----|----|
    | | | | |
    | 2 | | 5 | 7 |
    | 8 |

        |     |           |     |
        |  2  |           |  4  |
        |-----|     1     |-----|
        |     |-----------|     |
        |  3  |     7     |  5  |
        |           6           |


    Here all no's(1...) are the dockwidgets. But I am facing following problems to do so:

    1. As DockWidgetAreas have only the following layout option:

    Qt::AllDockWidgetAreas DockWidgetArea_Mask

    then how can I add the position for other windows.

    1. Positioning of the 'QDockWidget': When I am positioning the docs using the move and resize functions then it does not work properly.

    I have done the following try:

    • I have tried the sizeHint() and save/load geometry but it does not worked.

    • I have tried the layout on centerwidget using splitter and gridlayout but it does not give me the proper docking feature as the dock windows was the child of centerwidget.

    Please provide me some idea or solution on this issue....thanks in advance...

  • Are you sure you really want QDockWidgets? These are meant to be rearranged/moved and even put as toplevel windows by the user.

    From what I see, I would suggest that you go with a grid layout, its items can span multiple columns or rows if needed. You can easily create it with Qt Designer (stand alone app) or the built in designer of Qt Creator.

  • Hi Volker...thanks for the reply.

    Actually I need the following behavior for the windows:

    • Dock/Undock.
    • Move and Auto-dock to some area while dragging the windows.

    that's why I have to use QDockWidget (Layout like a visual studio IDE).

  • Seems to me that this is not possible. There is only one "row" of dock widgets designated around the central widget of a QMainWindow.

  • I think I've been able to build both of the OP's layouts by flagging AllowNestedDocks in the dockOptions section of the QMainWindow's properties - though, I did that at runtime, just by moving dockwidgets around, I have no idea about how to get the same results with plain code... HTH!

  • Uhm... not exactly the same layouts, but something along the lines... anyway, nested docks are definitely something to play with :-)

    just for a visual hint about what is certainly possible at runtime, here is one of the layouts I've been able to compose:
    | | | | | | |
    | 1 | 2 | | 5 | 6 | 7 |
    | | | central | | | |
    +---+---+ +---+-+-+---+
    | | | widget | | |
    | 3 | 4 | | 8 | 9 |
    | | | | | |
    | | |
    | 10 | 11 |
    | | |

  • Found it: you can define the disposition of those nested docks by repeatedly calling QMainWindow::splitDockWidget() and passing to it two docks each time (the one whose space you want to split, and the one you want to add as a sibling, along with the orientation of the arrangement you want to get).

    It may take some time to figure out the exact order of calls, but it can be done (you don't even need to activate the option to allow nested docks, as it seems it only affects the manual arrangement at runtime).

    A snippet:
    void MainWindow::on_pushButton_clicked() {
    splitDockWidget(ui->dockWidget, ui->dockWidget_3, Qt::Horizontal);
    splitDockWidget(ui->dockWidget, ui->dockWidget_2, Qt::Vertical);

    Something like the above will work (assuming appropriate context) to arrange the first three docks of the first OP's layout.

    The above function calls will produce different results (or no results at all) depending on the state and position of each dock, so you might need to explicitly un-float or move to the appropriate side each dock, before splitting the space they occupy.

  • Hi entuland, thanks for the reply…

    that helps a lot…thanks for this…

    In the above way, can I make “central widget” also as a QDockWidget, while doing this I am facing following constraints:

    Constraint1: I have to make “centralwidget” as a parent of QDockWidget. But in this case docking/undocking not works properly (because centralwidget is the parent not mainwindow).

    Constraint2: If I use setCentralWidget(0) then I have docking size/resize issue.

    Constraint3: One more thing here that “centralwidget” always take the huge space of the layout. If there a way to restrict it or properly managed.**

  • Moderators

    Do you really want 11 dockwindow areas? As a user I would get terribly confused by that!

  • @pastispast: you're welcome, I'm glad to help when I can.

    Unfortunately I cannot help you further with the issues of your layout, I can only suggest you to put something different as a central widget, not a dockwidget, so that your users are well aware of what is the main content of your program and where the docks can be put - also, practice a bit more with the interface, your constraint 3 does not stand: it's you who decide how much space the central widget takes, you can put a lot of stuff in the central widget yet make it completely disappear (that is, 0x0 size) by just resizing the docking areas, assuming you use appropriate settings for the size restrictions.

    @Tobias Hunger: it all depends on the type of the program and on the intended audience, I feel comfortable with lots of docks as long as I'm able to move, resize and hide them at will - just like you would feel comfortable with, I think - but if I was Average User, I think I could get confused too... only the OP can decide whether such an amount of docks is appropriate or not, since we don't know the context of the program at hand.

  • Actually, I'm missing something like Adobe Premiere dockings in Qt :-)

  • Unfortunately I never used Adobe Premiere... could you describe the features you're thinking about, peppe?

  • See f.i. , basically there are ONLY docked widgets with an unique look and feel, and quick menus everywhere to switch their layouts and add/remove them on the fly (with many presets available).

  • Looks quite interesting... well maybe we could subclass QDockWidget and add some functionality... it may become an interesting spare time project :-)

  • heads up, please use fixed width fonts when trying to convey design layouts to others, or put it in an image, because variable-width fonts hardly ever line up as they do for you on the other end (even if it does go ok in fixed width, but not everyone sees it this way). Maybe someone can fix this problem on the 'code view' either that or i'm the only one, if I am, just smack me.

  • Any update on this? Is there a way to have only dockable windows like the adobe example?

  • Moderators

    @10QT said:

    Is there a way to have only dockable windows like the adobe example?

    To some degree - yes. The key is creating a dummy 0 size central widget and enabling nested docks dockable to only single dock area (e.g. top). Here's an example:

    #include <QMainWindow>
    #include <QDockWidget>
    #include <QApplication>
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        QMainWindow w;
        w.setDockOptions(QMainWindow::AllowNestedDocks | QMainWindow::AllowTabbedDocks);
        w.setCentralWidget(new QWidget());//just a dummy
        auto d1 = new QDockWidget("Foo");
        w.addDockWidget(Qt::TopDockWidgetArea, d1);
        auto d2 = new QDockWidget("Bar");
        w.addDockWidget(Qt::TopDockWidgetArea, d2);
        auto d3 = new QDockWidget("Bazz");
        w.addDockWidget(Qt::TopDockWidgetArea, d3);
        auto d4 = new QDockWidget("Hello");
        w.addDockWidget(Qt::TopDockWidgetArea, d4);
        auto d5 = new QDockWidget("Bye");
        w.addDockWidget(Qt::TopDockWidgetArea, d5);
        auto d6 = new QDockWidget("Hi there");
        w.addDockWidget(Qt::TopDockWidgetArea, d6);
        w.splitDockWidget(d1, d2, Qt::Vertical);
        w.splitDockWidget(d3, d4, Qt::Vertical);
        w.splitDockWidget(d3, d5, Qt::Horizontal);
        w.tabifyDockWidget(d1, d6);
        return a.exec();

    This results in:
    Nested docks layout

    Presets and buttons for switching docks positions you can get by subclassing QDockWidget and customizing it.

  • Thanks for the quick reply, I'll give this a try

Log in to reply