Important: Please read the Qt Code of Conduct -

How to keep the QDialog in the screen without desappearing when it loses the focus

  • Hi,

    How can i keep a modeless QDialog on the screen even when it loses the focus?

  • @frnklu20
    Show your code. It should stay on the screen. Unless maybe your dialog is getting deleted/going out of scope in your code.

  • Have you tried Implementing QDialog::show() in FocusOutEvent?

  • I suppose by desappearing you mean that you would like to prevent the dialog to be obscured by another window.
    I think the Qt::Tool windowflag could be of interest:

    Popup | Dialog
    Indicates that the widget is a tool window. A tool window is often a small window with a smaller than usual title bar and decoration, 
    typically used for collections of tool buttons. If there is a parent, the tool window will always be kept on top of it. If there isn't a parent, 
    you may consider using Qt::WindowStaysOnTopHint as well. If the window system supports it, 
    a tool window can be decorated with a somewhat lighter frame. 
    It can also be combined with Qt::FramelessWindowHint. On OS X, tool windows correspond to the Floating class of windows. 
    This means that the window lives on a level above normal windows; it impossible to put a normal window on top of it. 
    By default, tool windows will disappear when the application is inactive. 
    This can be controlled by the Qt::WA_MacAlwaysShowToolWindow attribute.

    Work well on Mac for me, not sure for the other OS.

  • Hi,

    That's it! My program is composed of a mainwindow and when i press a button it opens a dialog, but i don't want that the dialog get obscured by the mainwindow when it loses focus.
    It's just to keep in the same position.


    When i click on the plus button this dialog, that have buttons appears, but even when it loses focus i want it to stay there and not get behind of the mainwindow.

    itemDialog::itemDialog(QWidget *parent) :
    { ......
             Qt::WindowFlags flags = 0;      
             flags = Qt::Tool;

    i tryed this, but i don't know if i'm implementing in the right way

  • @frnklu20
    That's not "disappearing from the screen when losing focus", it's just another window coming on top! Had no idea you could mean that!

    No, your code won't do anything, it simply sets an (unused) local variable. From where you are now replace by:

    setWindowFlags(windowFlags()  | Qt::Tool);

  • hi,

    i found another solution, instead of creating another class and implementing it, i created a QDialog inside of the mainwindow
    and used the mainwindow as a parent.

    Thanks guys, you're awesome

Log in to reply