Important: Please read the Qt Code of Conduct -

Losing Focus on modal QDialog (Qt5)

  • In my application, I have to often to display a dialog to the user.

    I use

    class MyDialog : public QDialog { }

    and to display to the user


    hence MyDialog is modal. In Microsoft Windows, this works as expected. However in Ubuntu (14.04) and OS X the following happens:

    • If the user clicks (accidently or on purpose) on the maindwindow, the focus of the dialog is lost. The focus is then on the main window (even though the main windows is blocked and does not accept any kind of input). Since the mainwindow gained focus, it is brought ontop of every window, i.e. hides the dialog (you can still bring back the dialog using the ubuntu launcher). Similar behavior occurs on OS X.

    This can be quite confusing for the user, as the mainwindow seems to be blcoked for no apparent reason.

    How can I prevent a modal QDialog from losing focus (actually, not losing focus should be the default behavior according to docu, but as said, this is not the case).

    Did I stumble upon a bug?

  • I suspect that you didn't pass the parent widget to the dialog. You don't show this in your code example so I am only guessing. Without a parent there is no reason for the dialog to stay on-top of anything else (although it might in some cases ?).

    I haven't noticed this problem by the way but I always construct dialog's with a parent (modal or not).

    MyDialog *dialog = new MyDialog(this);

  • Indeed, that's what I forogt. Thank you so much!

Log in to reply