Minimising parent widget window without minimising its children
-
Background
I'm developing a trading tool for our traders, and the tool contains a summary QTableWidget panel shows general info and traders can click the button in related row to open a order panel. The trader wants that the order panel can be shown always regardless of the summary panel is minimised or hide (except closed, in which case all order panels should be closed as well).
Problem
But the difficulty here is the order panels always gets minimised when I minimise the summary panel.
Simplified code
Below is a simplified demo to illustrate the problem
#include <QtWidgets/QWidget> #include <QtWidgets/QPushButton> #include <QtWidgets/QLabel> #include <QtWidgets/QApplication> #include <QEvent> #include <QtWidgets/QVBoxLayout> class ChildWidget : public QWidget { public: ChildWidget(QWidget* parent = nullptr) : QWidget(parent) { setWindowTitle("Child Widget"); setMinimumWidth(500); auto layout = new QVBoxLayout(this); auto label = new QLabel("Child Widget", this); layout->addWidget(label); } /* bool event(QEvent* myevent) override { if (myevent->type() == QEvent::WindowStateChange) { if (windowState() & Qt::WindowMinimized) { setWindowState(windowState() & ~Qt::WindowMinimized); } } return QWidget::event(myevent); } */ }; class MainWindow : public QWidget { public: MainWindow(QWidget* parent = nullptr) : QWidget(parent) { setWindowTitle("Main Window"); setMinimumWidth(500); auto layout = new QVBoxLayout(this); auto button = new QPushButton("Open Child", this); layout->addWidget(button); connect(button, &QPushButton::clicked, this, &MainWindow::show_child); } public: void show_child() { auto child = new ChildWidget(this); child->setWindowFlag(Qt::Window); child->show(); } }; int main(int argc, char* argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); return app.exec(); }I did some googling and asked github copilot, which suggest a solution of block minimising event as the commented part in
ChildWidget, but unfortunately this doesn't work. Even it blocks the minimising button on the order panel, i.e. the order panels cannot be minimised with itself, but still, when the summary panel gets minimised, all order panels gets minimised as well.The only way I have found now is to unlink the parent-child relationship of the summary panel and order panels, i.e., when creating
ChildWidget, we usenullptrfor itsparentparam (instead ofthis). But doing this, closing parent panelMainWidgetdoesn't close child panel. I will need to re-implement all WindowState related event handling other than minimising.So could someone have better way of implementing this?
Thanks in advance!
-
Background
I'm developing a trading tool for our traders, and the tool contains a summary QTableWidget panel shows general info and traders can click the button in related row to open a order panel. The trader wants that the order panel can be shown always regardless of the summary panel is minimised or hide (except closed, in which case all order panels should be closed as well).
Problem
But the difficulty here is the order panels always gets minimised when I minimise the summary panel.
Simplified code
Below is a simplified demo to illustrate the problem
#include <QtWidgets/QWidget> #include <QtWidgets/QPushButton> #include <QtWidgets/QLabel> #include <QtWidgets/QApplication> #include <QEvent> #include <QtWidgets/QVBoxLayout> class ChildWidget : public QWidget { public: ChildWidget(QWidget* parent = nullptr) : QWidget(parent) { setWindowTitle("Child Widget"); setMinimumWidth(500); auto layout = new QVBoxLayout(this); auto label = new QLabel("Child Widget", this); layout->addWidget(label); } /* bool event(QEvent* myevent) override { if (myevent->type() == QEvent::WindowStateChange) { if (windowState() & Qt::WindowMinimized) { setWindowState(windowState() & ~Qt::WindowMinimized); } } return QWidget::event(myevent); } */ }; class MainWindow : public QWidget { public: MainWindow(QWidget* parent = nullptr) : QWidget(parent) { setWindowTitle("Main Window"); setMinimumWidth(500); auto layout = new QVBoxLayout(this); auto button = new QPushButton("Open Child", this); layout->addWidget(button); connect(button, &QPushButton::clicked, this, &MainWindow::show_child); } public: void show_child() { auto child = new ChildWidget(this); child->setWindowFlag(Qt::Window); child->show(); } }; int main(int argc, char* argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); return app.exec(); }I did some googling and asked github copilot, which suggest a solution of block minimising event as the commented part in
ChildWidget, but unfortunately this doesn't work. Even it blocks the minimising button on the order panel, i.e. the order panels cannot be minimised with itself, but still, when the summary panel gets minimised, all order panels gets minimised as well.The only way I have found now is to unlink the parent-child relationship of the summary panel and order panels, i.e., when creating
ChildWidget, we usenullptrfor itsparentparam (instead ofthis). But doing this, closing parent panelMainWidgetdoesn't close child panel. I will need to re-implement all WindowState related event handling other than minimising.So could someone have better way of implementing this?
Thanks in advance!
@booksword said in Minimising parent widget window without minimising its children:
closing parent panel MainWidget doesn't close child panel
You can override https://doc.qt.io/qt-6/qwidget.html#closeEvent in MainWindow and close ChildWidget there.
-
@booksword said in Minimising parent widget window without minimising its children:
closing parent panel MainWidget doesn't close child panel
You can override https://doc.qt.io/qt-6/qwidget.html#closeEvent in MainWindow and close ChildWidget there.
@jsulm Thanks for answering.
Yes, this is my current solution, but I'm wondering if there is more elegant method to do so. Since just because I don't want to make child minimised with parent, I'll need to overwrite closeEvent and add all other actions one can do with a window (close, maximise etc) seems not reasonable.