5
votes

I want a dialog which stays on top of my main window and not other windows. I derived a class and added some flags. If I call the dialog now with show() the dialog appears and is staying on top as long as I don't press a button or whatever. Then the dialog goes to background again.

Dial::Dial(QWidget *parent) : QWidget(parent) 
{
  this->setWindowFlags(Qt::Tool | Qt::Dialog);
  // ...

Consequently, I looked into the docu and found this:

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.

Happily, I added this line into my singleton creating the dialog.

d->mainWindow = new Foo();
d->dial->setParent(d->mainWindow);

Now the dialog is just embedded into my central widget (QOpenGlWidget) and is not a dialog anymore. Somehow, I seem to lack understanding what the docu is telling me? How can I get the dialog stay on top of my application and what does the docu mean?

enter image description here

4
why are you using a QWidget instead of QDialog?msrd0
QDialog behaves basically the same. I just tested.dgrat
There is no platform independent way to do this. I spent a lot of time at my last job trying to find a solution to this problem. You can achieve this effect on windows with some combination of windowflags and similar, but not the right effect on mac with some different combination of windowflags. The behavior on mac also is OS version dependent. If you are fine with the popup disappearing when the application loses focus and reappearing when it gains focus, windowflags can work.SteakOverflow
A pity, I already expected there is no "clean" fix for that.dgrat

4 Answers

4
votes

I'm not able to reproduce your problem. The following code will generate a QWidget that will allways stay on top of the QMainWindow:

#include "QApplication"
#include "QMainWindow"
#include "QLineEdit"
int main(int argc, char * argv[])
{
    QApplication a(argc, argv);

    QMainWindow w;
    w.show ();

    QWidget *pLineEdit = new QWidget(&w);
    pLineEdit->setWindowFlags(Qt::Tool | Qt::Dialog);
    pLineEdit->show ();

    a.exec ();
}

Tested with Qt 5.9.

2
votes

Not sure if you've already solved this by now but you can try the WindowStaysOnTopHint flag when you construct the dialog:

Qt::WindowFlags flags = this->windowFlags();
flags |= Qt::WindowStaysOnTopHint;
this->setWindowFlags(flags);

Then use show() instead of exec() to make it non-modal:

dlg->show();
1
votes

You need to set the modality (documentation) of the widget, like this:

QWidget *dialog = new QWidget(window, Qt::Dialog);
dialog->setWindowModality(Qt::ApplicationModal);
dialog->show();

However, I'd recommend to use the pre-configured QDialog class, which handles all that stuff for you:

QDialog *dialog = new QDialog(window);
dialog->exec();
0
votes

Use QDialog instead of QWidget, and pass the parent widget in its constructor function.

QDialog* pDlg = new QDialog(this);
pDlg->show();