Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Window always on top but don't block input from others



  • Hi,

    i want to have a window that stays always on top of my MainWindow, but it shouldn't block the input from it.
    OpenFileDialogs shouldn't be affected. So if i have my subwindow open, it stays over my MainWindow, but not over the FileOpenDialog.

    i tried Qt::WindowStaysOnTopHint, but this makes it stay always on top and not only in top of my MainWindow.

    using WindowModal and ApplicationModal both block the interaction with MainWindow.

    I hope someone can help me :D


  • Moderators

    You're overcomplicating it. Just make the window child of the main window:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        MainWindow w;
        w.show();
    
        QDialog* dialog = new QDialog(&w);
        dialog->show();
    
        QFileDialog::getExistingDirectory(&w, "Hello");
    
        return a.exec();
    }
    

    or, if you want to use a plain QWidget instead of QDialog:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        MainWindow w;
        w.show();
    
        QWidget* widget = new QWidget(&w, Qt::Window);
        widget->show();
    
        QFileDialog::getExistingDirectory(&w, "Hello");
    
        return a.exec();
    }
    


  • @Chris-Kawa sometimes the easy way is the best way :D

    Just one question i changed the palette from the QScrollArea of my subwindow, so the background is painted white.
    Now it doesn't work anymore. I don't know why. Any ideas??

    Already tried this:

    ui->scrollArea->setStyleSheet("QScrollArea{background-color: #ffffff}");
    

    But does not work.


  • Moderators

    Oh it works. It's just covered by other widgets. Inside a scroll area there's a viewport widget and in it a contents widget.
    You can do what you want in many ways. For example you can narrow your stylesheet to apply to two widgets down:

    ui->scrollArea->setStyleSheet("QScrollArea>QWidget>QWidget {background-color: #ffffff}");
    

    Unfortunately that will also color the scrollbars, but you can instead style the content directly:

    ui->scrollArea->widget()->setStyleSheet("background-color: #ffffff");
    

    or be even more specific by giving it a name:

    ui->scrollArea->widget()->setObjectName("content");
    ui->scrollArea->setStyleSheet("#content {background-color: #ffffff}");
    

    Yet another alternative, if you really want to color the scrollarea itself, is to stop Qt from drawing the background of the content:

    ui->scrollArea->setStyleSheet("QScrollArea{background-color: #ffffff}");
    ui->scrollArea->widget()->setAutoFillBackground(false);
    

    Btw. Changing palette colors is wobbly at best. Not all styles use the palette for all elements.


Log in to reply