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. Window is not in focus when opened

Window is not in focus when opened

Scheduled Pinned Locked Moved Solved General and Desktop
qwidgetwindow
14 Posts 2 Posters 7.7k Views 2 Watching
  • 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I'm opening a window widget from code. When that happens I already have 2 dialogs open.

    QHBoxLayout *rev_Title = new QHBoxLayout;
    
        QFont f( "Arial", 18, QFont::Bold);
    	revTitle->setFont (f);
        revTitle->setText ("Review the Data You Entered");
        rev_Title->addWidget (revTitle,Qt::AlignCenter);
    
        review_Window->setWindowTitle ("Review");
        rev_Layout->addLayout (rev_Title);
    
        review_Window->isModal ();
        
        review_Window->show ();
        
        review_Window->setFocus ();
    

    Everything works nicely, but the new window is hidden between the 2 previously opened windows so it is not in the foreground. How can I open it in a way that it stays in the foreground and has the focus on it?

    Thank you.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi
      you can try to call
      http://doc.qt.io/qt-5/qwidget.html#raise
      on it.

      If you mean stay on top at ALL times then
      there is a flag for that
      Qt::WindowStaysOnTopHint
      you can use with setWindowFlags
      -BUT- it makes windows stay above all others all the time.
      Not sure you want that.

      1 Reply Last reply
      1
      • G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Thank you.
        I made the following changes:

            setWindowFlags (Qt::Popup);
            review_Window->setWindowTitle ("Review");
        

        The new window does stay on top. I have 3 dialogs open:
        mainwindow
        additem
        review_Window

        Review_window is generated by additem. When review_window opens (and stays on top) it closes additem which I still need open. How can I keep additem open?
        Thank you.

        mrjjM 1 Reply Last reply
        0
        • G gabor53

          Thank you.
          I made the following changes:

              setWindowFlags (Qt::Popup);
              review_Window->setWindowTitle ("Review");
          

          The new window does stay on top. I have 3 dialogs open:
          mainwindow
          additem
          review_Window

          Review_window is generated by additem. When review_window opens (and stays on top) it closes additem which I still need open. How can I keep additem open?
          Thank you.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @gabor53
          Hi you must show the code for how you open
          additem and review_Window
          as there is no reason for review_window to close additem so I assume
          its something with your code. Like using exec instead of show or
          using a local variable for additem so it runs out of scope and be deleted.

          G 1 Reply Last reply
          1
          • mrjjM mrjj

            @gabor53
            Hi you must show the code for how you open
            additem and review_Window
            as there is no reason for review_window to close additem so I assume
            its something with your code. Like using exec instead of show or
            using a local variable for additem so it runs out of scope and be deleted.

            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            @mrjj
            Here is the code opening additem:

                Additem mAddItem;
                mAddItem.setModal (true);
                mAddItem.exec ();
            

            and this opens review_Window:

                review_Window->isModal ();
            
                review_Window->show ();
            
                review_Window->setFocus ();
            

            Thank you for your help.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @gabor53 said:

              Hi
              Additem mAddItem;
              is local variable it seems but you are using exec() should should not run out of scope.
              Please put a break point on next line after
              mAddItem.exec ();

              and see if it for some reason falls out of exec() when u open review_Window.

              Do you use a slot/button to call the code that opens review_Window?

              G 1 Reply Last reply
              1
              • mrjjM mrjj

                @gabor53 said:

                Hi
                Additem mAddItem;
                is local variable it seems but you are using exec() should should not run out of scope.
                Please put a break point on next line after
                mAddItem.exec ();

                and see if it for some reason falls out of exec() when u open review_Window.

                Do you use a slot/button to call the code that opens review_Window?

                G Offline
                G Offline
                gabor53
                wrote on last edited by
                #7

                @mrjj
                Hi,

                    Additem mAddItem;
                    mAddItem.setModal (true);
                    mAddItem.exec ();
                    break;
                
                

                generated an error message: break statement is not within a loop or switch.

                Yes, the review_Window is opened in a function triggered by a slot/button.

                Thank you.

                mrjjM 1 Reply Last reply
                0
                • G gabor53

                  @mrjj
                  Hi,

                      Additem mAddItem;
                      mAddItem.setModal (true);
                      mAddItem.exec ();
                      break;
                  
                  

                  generated an error message: break statement is not within a loop or switch.

                  Yes, the review_Window is opened in a function triggered by a slot/button.

                  Thank you.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @gabor53
                  hi
                  i imagined something like
                  mdditem mAddItem;
                  mAddItem.setModal (true);
                  mAddItem.exec ();
                  int a=100; <<< break point here.

                  G 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @gabor53
                    hi
                    i imagined something like
                    mdditem mAddItem;
                    mAddItem.setModal (true);
                    mAddItem.exec ();
                    int a=100; <<< break point here.

                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #9

                    @mrjj
                    Hi
                    I tried.

                        Additem mAddItem;
                        mAddItem.setModal (true);
                        mAddItem.exec ();
                        int a = 100;
                    

                    Nothing really happened except I got a message saying unused variable.

                    mrjjM 1 Reply Last reply
                    0
                    • G gabor53

                      @mrjj
                      Hi
                      I tried.

                          Additem mAddItem;
                          mAddItem.setModal (true);
                          mAddItem.exec ();
                          int a = 100;
                      

                      Nothing really happened except I got a message saying unused variable.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @gabor53
                      hi
                      did it stop at break point when review_window open?
                      meaning it went from
                      mAddItem.exec (); << should stay here
                      int a = 100; << should not go to this line when review_window

                      also
                      review_Window->show ();

                      this is how u open review_Window still ?

                      Else Im afraid I cant guess what you are doing wrong.
                      Normally its not an isses to have 2 dialogs so must be some small details.
                      So u are almost there:)

                      G 1 Reply Last reply
                      2
                      • mrjjM mrjj

                        @gabor53
                        hi
                        did it stop at break point when review_window open?
                        meaning it went from
                        mAddItem.exec (); << should stay here
                        int a = 100; << should not go to this line when review_window

                        also
                        review_Window->show ();

                        this is how u open review_Window still ?

                        Else Im afraid I cant guess what you are doing wrong.
                        Normally its not an isses to have 2 dialogs so must be some small details.
                        So u are almost there:)

                        G Offline
                        G Offline
                        gabor53
                        wrote on last edited by
                        #11

                        @mrjj
                        Hi
                        It did not stop at breakpoint.
                        I open review_Window like this:

                            review_Window->setFocus ();
                            review_Window->show ();
                        

                        New development:
                        I haven't changed anything. Now all 3 windows (mainwindow, additem, review_Window) stay open. They are stacked from top to bottom like this: additem, review_Window, mainwindow. The minimize button doesn't work when all 3 windows open. If I close one of them everything works. No idea why. Thank you for your help.

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi
                          When you call exec on a dialog, it become modal.
                          This means that other windows are blocked. also including minimize button.
                          http://www.informit.com/articles/article.aspx?p=1405225&seqNum=5

                          That might be the reason.

                          G 1 Reply Last reply
                          2
                          • mrjjM mrjj

                            Hi
                            When you call exec on a dialog, it become modal.
                            This means that other windows are blocked. also including minimize button.
                            http://www.informit.com/articles/article.aspx?p=1405225&seqNum=5

                            That might be the reason.

                            G Offline
                            G Offline
                            gabor53
                            wrote on last edited by
                            #13

                            @mrjj
                            Thank you. I redid the code and it works now.

                            mrjjM 1 Reply Last reply
                            2
                            • G gabor53

                              @mrjj
                              Thank you. I redid the code and it works now.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @gabor53
                              good work!

                              1 Reply Last reply
                              1

                              • Login

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