Window is not in focus when opened



  • 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.


  • Qt Champions 2016

    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.



  • 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.


  • Qt Champions 2016

    @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.



  • @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.


  • Qt Champions 2016

    @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?



  • @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.


  • Qt Champions 2016

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



  • @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.


  • Qt Champions 2016

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



  • @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.


  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

    @gabor53
    good work!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.