Set QWidget on top of current application but not other application

Viewed 14748

I have an application with several windows. Each window is a QWidget without parent.

I want those QWidget to be on top of the application, but not on top of other application. It's like the windows in Visual Studio, for instance, when they are free. They can't be hidden by the main window, but can be by an other application.

I tried with "setWindowFlags(Qt::WindowStaysOnTopHint);" , but it keeps the QWidget on top of all the applications.

3 Answers

Here's what worked for me:

I have a non-modal QDialog that I want to stay in front of my app, but only my app. Before showing the dialog I set the application's QMainWindow as its parent:

foreach( QWidget* p_widget, qApp->topLevelWidgets() )
{
    if ( QMainWindow* p_main_window = qobject_cast<QMainWindow*>( p_widget ) )
    {
        m_p_settings_dialog->setParent( p_main_window );
    }
}

Then I set the flags:

m_p_settings_dialog->setWindowFlags( Qt::Window
                                     | Qt::CustomizeWindowHint
                                     | Qt::WindowTitleHint
                                     | Qt::WindowCloseButtonHint );

And then I show the dialog.

Now it works flawlessly for me. No need to set Qt::WindowStaysOnTopFlag at all. Good luck!

Related