Results 1 to 1 of 1

Thread: QMainWindow::restoreState doesn't bring floating QDockWidgets to the front

  1. #1
    Join Date
    Oct 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QMainWindow::restoreState doesn't bring floating QDockWidgets to the front

    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.

    Qt Code:
    1. void App::show()
    2. {
    3. QMainWindow* _main = new QMainWindow();
    4.  
    5. QDockWidget* _table = new QDockWidget(_main);
    6. _table->setObjectName("table");
    7. _main->addDockWidget(Qt::RightDockWidgetArea, _table);
    8.  
    9. _main->restoreState();
    10.  
    11. _table->activateWindow(); // bring QDockWidget to the front
    12. _main->activateWindow(); // make QMainWindow the active window
    13.  
    14. _main->show();
    15. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void MyMainWindow::show()
    2. {
    3. QList<QDockWidget*> dock_widgets = findChildren<QDockWidget*>();
    4.  
    5. for (QDockWidget* dock_widget : dock_widgets)
    6. {
    7. dock_widget->activateWindow();
    8. }
    9.  
    10. activateWindow();
    11. QMainWindow::show();
    12. }
    To copy to clipboard, switch view to plain text mode 

    It still seems a bit broken though


    Added after 1 25 minutes:


    Fixed!

    The problem is restoring state before showing the window

    Qt Code:
    1. void App::show()
    2. {
    3. QMainWindow* _main = new QMainWindow();
    4.  
    5. QDockWidget* _table = new QDockWidget(_main);
    6. _table->setObjectName("table");
    7. _main->addDockWidget(Qt::RightDockWidgetArea, _table);
    8.  
    9. _main->show();
    10.  
    11. _main->restoreState(); // only restore state **after** showing the window
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by skebanga; 23rd October 2015 at 18:14.

Similar Threads

  1. Floating QDockWidget not re-docked by QMainWindow::restoreState
    By stefanadelbert in forum Qt Programming
    Replies: 14
    Last Post: 20th October 2016, 22:16
  2. Replies: 0
    Last Post: 8th November 2012, 18:09
  3. Replies: 1
    Last Post: 22nd February 2012, 14:55
  4. Replies: 1
    Last Post: 28th October 2008, 17:29
  5. How to bring a dialogbox to the front?
    By bood in forum Qt Programming
    Replies: 2
    Last Post: 2nd August 2006, 16:04

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.