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. How to force QDialogue to be on top/not on top of MainWindow
Forum Updated to NodeBB v4.3 + New Features

How to force QDialogue to be on top/not on top of MainWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 809 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.
  • L Offline
    L Offline
    leinad
    wrote on last edited by
    #1

    Hi,

    I have a QDialoge which I created that uses qwt for plotting graphs. When I launch the dialogue I want to give users the choice of making the dialogue always stay on top or not. I setup a signal/slot with a check box to force the dialogue to change its flags but for some reason it doesn't work. Ideally what I would really prefer is to have the dialogue have a pin which is much nicer but without being part of a dock since the dialogue is not designed to dock to the main window.

    If someone can show me how to create a pin even with docking but ignore the docking feature just enable the pin, that would be great but if not here is the code for forcing the dialogue to change its flags when the user checks and unchecks the checkbox to force the dialogue always on top or not. Perhaps you can't do it on the fly. The signal/slot works so I'm only showing the slot code.

    void MainWindow::getAlwaysOnTopFlag(int widgetID, bool alwaysOnTopFlag)
    {
    if(alwaysOnTopFlag == true)
    {
    dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint); //clear the past flags (not sure if correct)
    dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
    dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
    dialogue->setWindowFlags(windowFlags() & Qt::WindowStaysOnTopHint); //always on top
    }
    else //not on top
    {
    dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint); //clear past flags
    dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
    dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
    dialogue->setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint); //not on top
    }
    }

    Any help would be greatly appreciated!
    Thanks.

    JonBJ 1 Reply Last reply
    0
    • L leinad

      Hi,

      I have a QDialoge which I created that uses qwt for plotting graphs. When I launch the dialogue I want to give users the choice of making the dialogue always stay on top or not. I setup a signal/slot with a check box to force the dialogue to change its flags but for some reason it doesn't work. Ideally what I would really prefer is to have the dialogue have a pin which is much nicer but without being part of a dock since the dialogue is not designed to dock to the main window.

      If someone can show me how to create a pin even with docking but ignore the docking feature just enable the pin, that would be great but if not here is the code for forcing the dialogue to change its flags when the user checks and unchecks the checkbox to force the dialogue always on top or not. Perhaps you can't do it on the fly. The signal/slot works so I'm only showing the slot code.

      void MainWindow::getAlwaysOnTopFlag(int widgetID, bool alwaysOnTopFlag)
      {
      if(alwaysOnTopFlag == true)
      {
      dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint); //clear the past flags (not sure if correct)
      dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
      dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
      dialogue->setWindowFlags(windowFlags() & Qt::WindowStaysOnTopHint); //always on top
      }
      else //not on top
      {
      dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint); //clear past flags
      dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
      dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
      dialogue->setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint); //not on top
      }
      }

      Any help would be greatly appreciated!
      Thanks.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @leinad said in How to force QDialogue to be on top/not on top of MainWindow:

      dialogue->setWindowFlags(windowFlags() & Qt::WindowStaysOnTopHint); //always on top

      The & looks wrong, you're not setting it. In fact, you're (probably) undoing the previous two | lines....?

      dialogue->setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); //always on top
      

      Like the other flags you set. | to set, & ~ to clear.

      In newer Qt versions there is QWidget::setWindowFlag(Qt::WindowType flag, bool on = true) which makes manipulating one flag easier if you prefer.

      1 Reply Last reply
      3
      • L Offline
        L Offline
        leinad
        wrote on last edited by
        #3

        When I set to always on top, the dialogue disappears. Seems Qt does not the flags to change.

        JonBJ 1 Reply Last reply
        0
        • L leinad

          When I set to always on top, the dialogue disappears. Seems Qt does not the flags to change.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @leinad
          I can only say that if you have not changed your shown code to what I wrote I don't know what you are actually setting its flags to when alwaysOnTopFlag == true (you are clearing all its flags). Have you altered your code?

          1 Reply Last reply
          1
          • L Offline
            L Offline
            leinad
            wrote on last edited by
            #5

            void MainWindow::getAlwaysOnTopFlag(int widgetID, bool alwaysOnTopFlag)
            {
            if(alwaysOnTopFlag == true)
            {
            dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint);
            dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
            dialogue->setParent(NULL); //allow the plotWidget not to go behind the main window
            dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
            dialogue->setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); //always on top
            }
            else
            {
            dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint);
            dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
            dialogue->setParent(NULL); //allow the plotWidget to go behind the main window
            dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
            dialogue->setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint); //not on top
            }
            }

            No matter what part of the if statement, the dialogue closes. Going to the any flag statement closes the dialogue. I commented one by one. So I have two issues. The dialogue closing and have the capability I want. Basically Qt does not like any of the flag statements whether it is for alwaysOnTop == true or false.

            JonBJ 1 Reply Last reply
            0
            • L leinad

              void MainWindow::getAlwaysOnTopFlag(int widgetID, bool alwaysOnTopFlag)
              {
              if(alwaysOnTopFlag == true)
              {
              dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint);
              dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
              dialogue->setParent(NULL); //allow the plotWidget not to go behind the main window
              dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
              dialogue->setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); //always on top
              }
              else
              {
              dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint);
              dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
              dialogue->setParent(NULL); //allow the plotWidget to go behind the main window
              dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
              dialogue->setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint); //not on top
              }
              }

              No matter what part of the if statement, the dialogue closes. Going to the any flag statement closes the dialogue. I commented one by one. So I have two issues. The dialogue closing and have the capability I want. Basically Qt does not like any of the flag statements whether it is for alwaysOnTop == true or false.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @leinad
              Is the following relevant:
              https://doc.qt.io/qt-5/qwidget.html#windowFlags-prop

              Note: This function [i.e. setWindowFlags()] calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again..

              1 Reply Last reply
              0
              • L Offline
                L Offline
                leinad
                wrote on last edited by
                #7

                Yes, I just figured that out. So this works for those interested.

                //Slot to force a plot widget to always be on top or not - alwaysOnTopFlag = true means always on top
                void MainWindow::getAlwaysOnTopFlag(int widgetID, bool alwaysOnTopFlag)
                {
                if(alwaysOnTopFlag == true)
                {
                dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
                dialogue->setParent(NULL); //allow the plotWidget not to go behind the main window
                dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint);
                dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
                dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
                dialogue->setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); //always on top
                dialogue->show();
                }
                else
                {
                dialogue->setWindowFlags(windowFlags() & ~Qt::CustomizeWindowHint);
                dialogue->setWindowFlags(windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
                dialogue->setParent(NULL); //allow the plotWidget to go behind the main window
                dialogue->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); //add a minimize button
                dialogue->setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint); //not on top
                dialogue->show();
                }
                }

                Would a pin be easier or better? If so is there an easy way to implement?

                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