Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-modal QWidget dialog that stays on top of the window

Tags:

c++

qt

qt5

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

like image 473
dgrat Avatar asked Sep 22 '17 11:09

dgrat


4 Answers

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.

like image 96
m7913d Avatar answered Nov 03 '22 20:11

m7913d


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();
like image 32
Brad Elliott Avatar answered Nov 03 '22 21:11

Brad Elliott


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();
like image 1
msrd0 Avatar answered Nov 03 '22 20:11

msrd0


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

QDialog* pDlg = new QDialog(this);
pDlg->show();
like image 1
Linker Avatar answered Nov 03 '22 19:11

Linker