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

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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      • Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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

          Joel BodenmannJ 1 Reply Last reply
          5
          • SGaistS SGaist

            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.

            Joel BodenmannJ Offline
            Joel BodenmannJ Offline
            Joel Bodenmann
            wrote on 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

            • Login

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