Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unexpected raise() behavior
QtWS25 Last Chance

Unexpected raise() behavior

Scheduled Pinned Locked Moved Solved General and Desktop
qwidgetraise
5 Posts 3 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 3 Jun 2016, 07:23 last edited by
    #1

    I have a method in my MainWindow subclass that creates a new QDockWidget. That newly created dock widget is added to an existing dock widget to tabify them by using QMainWindow::tabifyDockWidget() and so far everything works.
    I have another method that calls QWidget::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 the raise() 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 the raise() 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?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    1
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 3 Jun 2016, 08:33 last edited by
      #2

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

      1 Reply Last reply
      2
      • J Offline
        J Offline
        Joel Bodenmann
        wrote on 3 Jun 2016, 09:30 last edited by
        #3

        Well that works. Might somebody explain what's going on? :) Seems like I am missing a basic concept of Qt.

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Jun 2016, 09:39 last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply 3 Jun 2016, 09:44
          5
          • S SGaist
            3 Jun 2016, 09:39

            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.

            J Offline
            J Offline
            Joel Bodenmann
            wrote on 3 Jun 2016, 09:44 last edited by
            #5

            Thank you for the help & the explanation. Very appreciated.

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            1 Reply Last reply
            0

            2/5

            3 Jun 2016, 08:33

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved