How to implement three level window (Windows)
-
Hi all,
I am trying to implement a dual screen application which should support 3 level (z-order) of windows.
At the base, there is main widget and its aux widget which is created as child of main widget and placed in another screen. Some of the buttons in any of these widgets can open a dialog(mid level) that should always stay on top of this base level, but should not be a modal dialog. And that the same time, the base level or the mid level dialog can open a blocking dialog which should be the most top level.
Below is a simplified example of my requirement.
// Widget.h #pragma once #include <QWidget> class MainWidget : public QWidget { Q_OBJECT public: MainWidget(QWidget *parent = nullptr); ~MainWidget(); virtual void setVisible(bool isVisible) override; private: QDialog* m_AuxDialog = nullptr; QDialog* m_Dialog = nullptr; };
// Widget.cpp #include "widget.h" #include <QDialog> #include <QHBoxLayout> #include <QPushButton> class MidLevelDialog : public QDialog { public: explicit MidLevelDialog(QWidget* p_Parent = nullptr) : QDialog(p_Parent) { setWindowFlags(windowFlags() | Qt::Tool); auto hLayout = new QHBoxLayout(this); auto btn = new QPushButton("Open Modal Dialog", this); hLayout->addWidget(btn); connect(btn, &QPushButton::clicked, this, []{ QDialog dlg; dlg.exec(); }); } virtual ~MidLevelDialog() { } }; MainWidget::MainWidget(QWidget *parent) : QWidget(parent) { m_AuxDialog = new QDialog(this); m_AuxDialog->setGeometry(100, 100, 300, 300); auto hLayout = new QHBoxLayout(this); auto btn = new QPushButton("Open Stays On Top Dialog", this); hLayout->addWidget(btn); connect(btn, &QPushButton::clicked, this, [this]{ if (!m_Dialog) { m_Dialog = new MidLevelDialog(m_AuxDialog); } m_Dialog->show(); }); setGeometry(400, 100, 300, 300); } MainWidget::~MainWidget() { } void MainWidget::setVisible(bool isVisible) { QWidget::setVisible(isVisible); m_AuxDialog->setVisible(isVisible); }
Some of the method I have tried but still not ideal:
- Create the mid level dialog with Qt::WindowsStaysOnTopHint. But this will cause the highest level modal dialog to go behind it.
- Create the mid level dialog with Aux widget as parent. Although this achieve the z-order requirement that I want, in reality its not that easy / correct to create this mid level widget with aux as parent. Another side effect of it that the mid level dialog always will be opened above the Aux widget which I do not want.
NOTE: This issue does not happen in Mac due to how Mac uses the Qt::Tool flag and modal dialog.
Please advice. Thanks in advance.
-
Maybe you can try using floating dock widgets as dialogs.