Unexpected raise() behavior
-
I have a method in my
MainWindow
subclass that creates a newQDockWidget
. That newly created dock widget is added to an existing dock widget to tabify them by usingQMainWindow::tabifyDockWidget()
and so far everything works.
I have another method that callsQWidget::raise()
on one of the tabs of the dock widget group and the tab is successfully raised to the foreground (-> becomes the selected tab).
The next thing I wanted to do is calling theraise()
method right after I added to the newly created dock widget to the dock widget group. Hence my code looks like this:void MainWindow::createDock() { // Find an existing dock that contains viewers QDockWidget* existingDock = nullptr; ... // Create a new dock QDockWidget* dock = new QDockWidget(this); addDockWidget(Qt::LeftDockWidgetArea, dock); // Tab them together if (existingDock) { tabifyDockWidget(existingDock, dock); } else { existingDock = dock; } // Focus the new dock dock->raise(); dock->setFocus(); }
But the newly created dock is never being focused/raised.
The next thing I did was adding a single shot timer that calls another method that calls theraise()
method:void MainWindow::createDock() { // Find an existing dock that contains viewers QDockWidget* existingDock = nullptr; ... // Create a new dock QDockWidget* dock = new QDockWidget(this); addDockWidget(Qt::LeftDockWidgetArea, dock); // Tab them together if (existingDock) { tabifyDockWidget(existingDock, dock); } else { existingDock = dock; } // Focus the new dock QTimer::singleShot(1, this, SLOT(highlightDock())); } void MainWindow::highlightDock() { dock->raise(); dock->setFocus(); dock->highlight(); }
And that actually works - I get the expected behavior.
I am having a hard time understanding what is going on. Obviously there must be something that prevents me from raising a widget that was just added but I don't understand what it is. Can somebody enlighten me?
-
Hi
Since it works if asked slightly later.
Could you try to insert
qApp->processEvents();
just before raise?
Just for test. should not matter.
But i like to test :) -
Well that works. Might somebody explain what's going on? :) Seems like I am missing a basic concept of Qt.
-
Hi,
Wild educated guess: you are calling raise right after creating a new widget but the event loop didn't got to run before you called raise thus you are calling raise on a yet to be shown widget.
Using the QTimer, you allow the event loop to run before calling raise, and it can then setup/show the widget.
-
Thank you for the help & the explanation. Very appreciated.