Frage

I have two QDockWidget's, only one of them is visible at the time, I manage that by toggleViewAction().

What I need to do is that I want the two QDockWidget's to be in the same location, with the same size and docked at the same dockWidgetArea with the same orientation and order relative to other QDockWidgets.

I did most of the that by this code:

void myMainWindow::slotToggleDocks(QAction* action) {
    if(action == viewDock1) {
    Dock1->setFloating(Dock2->isFloating());
    Dock1->resize(Dock2->size());
    Dock1->restoreGeometry(Dock2->saveGeometry());
    Dock1->move(Dock2->pos());
    addDockWidget(dockWidgetArea(Dock2), Dock1);
    ...
        Dock2->hide();

    } else if(action == viewDock2) {
    Dock2->setFloating(Dock1->isFloating());
    Dock2->resize(Dock1->size());
    Dock2->restoreGeometry(Dock1->saveGeometry());
    Dock2->move(Dock1->pos());
    addDockWidget(dockWidgetArea(Dock2), Dock1);
    ...
        Dock1->hide();
    }
}

this code make the two have the same location and size and docked to the same area (left, right, ...) but it doesn't guarantee that the Docks will have the same layout with the other QDockWidget's in the same dockWidgetArea.

Meaning if this was the before layout:

Layout before http://holmez.net/qdockwidget/1.png

This is after toggling:

Layout after toggling http://holmez.net/qdockwidget/2.png

This is what I want:

Expected result http://holmez.net/qdockwidget/3.png

War es hilfreich?

Lösung

I managed to do that by a simple trick, add this line of code:

splitDockWidget(Dock1,Dock2,Qt::Horizontal);

before hiding Dock1, and this line:

splitDockWidget(Dock2,Dock1,Qt::Horizontal);

before hiding Dock2.

This fixed the problem of the arrangement of docked widgets, not sure if this is the best way but it works for me.

Andere Tipps

What about only using 2 QDockWidgets, but having QStackedWidgets INSIDE of them which you can use to swap views? That's what I'm currently doing, and it works great.

Another advantage is that swapping views is as simple as:

    stackedWidget->setCurrentIndex(index);

What about using QTabWidget? The 2 widgets will have to be in the same Qt::DockWidgetAreas

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top