|
| 1 | +#pragma once |
| 2 | +#include <QApplication> |
| 3 | +#include <QCloseEvent> |
| 4 | +#include <QCheckBox> |
| 5 | +#include <QFrame> |
| 6 | +#include <QMainWindow> |
| 7 | +#include <QPushButton> |
| 8 | + |
| 9 | +namespace Examples { |
| 10 | + class Window2 : public QMainWindow { |
| 11 | + Q_OBJECT |
| 12 | + signals: |
| 13 | + bool windowClosing(); |
| 14 | + void windowClosed(); |
| 15 | + |
| 16 | + protected: |
| 17 | + void closeEvent(QCloseEvent* event) override { |
| 18 | + event->setAccepted(emit windowClosing()); |
| 19 | + if (event->isAccepted()) emit windowClosed(); |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + class Window1 : public QMainWindow { |
| 24 | + Q_OBJECT |
| 25 | + ; |
| 26 | + public: |
| 27 | + Window1() { |
| 28 | + resize(320, 325); |
| 29 | + setWindowTitle("Window1 show and hide example"); |
| 30 | + setCentralWidget(&frame); |
| 31 | + |
| 32 | + window2.resize(300, 100); |
| 33 | + window2.setWindowTitle("Close count = 0"); |
| 34 | + window2.connect(&window2, &Window2::windowClosing, [&] {return !cancelCloseCheckBox.isChecked();}); |
| 35 | + window2.connect(&window2, &Window2::windowClosed, [&] {window2.setWindowTitle(QString {"Close count = %1"}.arg(++closedCount));}); |
| 36 | + |
| 37 | + closeButton.move(10, 10); |
| 38 | + closeButton.resize(100, 40); |
| 39 | + closeButton.connect(&closeButton, &QPushButton::clicked, [&] {window2.close();}); |
| 40 | + |
| 41 | + showButton.move(10, 60); |
| 42 | + showButton.resize(100, 40); |
| 43 | + showButton.connect(&showButton, &QPushButton::clicked, [&] {window2.show();}); |
| 44 | + |
| 45 | + hideButton.move(10, 110); |
| 46 | + hideButton.resize(100, 40); |
| 47 | + hideButton.connect(&hideButton, &QPushButton::clicked, [&] {window2.hide();}); |
| 48 | + |
| 49 | + cancelCloseCheckBox.move(10, 160); |
| 50 | + cancelCloseCheckBox.resize(100, 40); |
| 51 | + } |
| 52 | + |
| 53 | + private: |
| 54 | + void closeEvent(QCloseEvent* event) override {QApplication::exit();} |
| 55 | + |
| 56 | + int closedCount = 0; |
| 57 | + Window2 window2; |
| 58 | + QFrame frame {this}; |
| 59 | + QPushButton closeButton {"Close", &frame}; |
| 60 | + QPushButton showButton {"Show", &frame}; |
| 61 | + QPushButton hideButton {"Hide", &frame}; |
| 62 | + QCheckBox cancelCloseCheckBox {"cancel close", &frame}; |
| 63 | + }; |
| 64 | +} |
0 commit comments