PDA

View Full Version : QMainWindow::restoreState doesn't bring floating QDockWidgets to the front



skebanga
23rd October 2015, 17:14
A floating dock widget is not automatically brought in front of existing windows from other applications when restoring main window state

I am using QMainWindow::restoreState() at application startup to successfully restore undocked QDockWidgets to their floating position.

The issue I have is that while QMainWindow is brought to the front, the floating QDockWidget is not.

This is illustrated by having a terminal window open, and starting my application from the command line.

If the floating QDockWidget is restored to a position that is in the same region as the terminal window, then it comes up below the terminal window. I have to move the terminal to reveal the QDockWidget.

Work around:

I have a work around in that I call QWidget::activateWindow() on the QDockWidget to bring it in front of my terminal, and then on the QMainWindow so that this is the active window, which would probably be the expected behaviour.



void App::show()
{
QMainWindow* _main = new QMainWindow();

QDockWidget* _table = new QDockWidget(_main);
_table->setObjectName("table");
_main->addDockWidget(Qt::RightDockWidgetArea, _table);

_main->restoreState();

_table->activateWindow(); // bring QDockWidget to the front
_main->activateWindow(); // make QMainWindow the active window

_main->show();
}


If I don't use QWidget::activateWindow() on the QDockWidget, then it's hidden behind the existing terminal window.

Is it possible to get this behaviour without having to iterate over all widgets calling QWidget::activateWindow()? As the number of widgets grows it will get somewhat onerous.

Added after 13 minutes:

I have made a somewhat feasible workaround by inheriting QMainWindow and overriding QMainWindow::show()



void MyMainWindow::show()
{
QList<QDockWidget*> dock_widgets = findChildren<QDockWidget*>();

for (QDockWidget* dock_widget : dock_widgets)
{
dock_widget->activateWindow();
}

activateWindow();
QMainWindow::show();
}


It still seems a bit broken though

Added after 1 25 minutes:

Fixed!

The problem is restoring state before showing the window



void App::show()
{
QMainWindow* _main = new QMainWindow();

QDockWidget* _table = new QDockWidget(_main);
_table->setObjectName("table");
_main->addDockWidget(Qt::RightDockWidgetArea, _table);

_main->show();

_main->restoreState(); // only restore state **after** showing the window
}