Skip to content

Commit 9f56613

Browse files
committed
Add WindowShowHide example
1 parent edc1742 commit 9f56613

File tree

8 files changed

+169
-4
lines changed

8 files changed

+169
-4
lines changed

Qt.Widgets/Others/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ add_subdirectory(InputDialogMultiline)
88
add_subdirectory(InputDialogPassword)
99
add_subdirectory(LCDNumber2)
1010
add_subdirectory(Wiggly)
11+
add_subdirectory(WindowShowHide)

Qt.Widgets/Others/Others.pro

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ SUBDIRS = \
44
InputDialogMultiline \
55
InputDialogPassword \
66
LCDNumber2 \
7-
Wiggly
7+
Wiggly \
8+
WindowShowHide

Qt.Widgets/Others/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
[This folder](.) contains others examples.
44

5-
* [ButtonWithStyleSheet](ButtonWithStyleSheet/README.md) show how to use stylesheet with QPushButton.
6-
* [InputDialogMultiline](InputDialogMultiline/README.md) show how to use multiline input dialog with QInputDialog.
7-
* [InputDialogPassword](InputDialogPassword/README.md) show how to use password input dialog with QInputDialog.
5+
* [ButtonWithStyleSheet](ButtonWithStyleSheet/README.md) shows how to use stylesheet with QPushButton.
6+
* [InputDialogMultiline](InputDialogMultiline/README.md) shows how to use multiline input dialog with QInputDialog.
7+
* [InputDialogPassword](InputDialogPassword/README.md) shows how to use password input dialog with QInputDialog.
88
* [LCDNumber2](LCDNumber2/README.md) shows how to create a LCD number with QLCDNumber.
99
* [Wiggly](Wiggly/README.md) shows how to animate a user control using QBasicTimer and timerEvent(). In addition, the example demonstrates how to use QFontMetrics to determine the size of text on screen.
10+
* [WindowShowHide](WindowShowHide/README.md) demonstrates the use of QMainWindow show, hide and close methods.
1011

1112
## Generate and build
1213

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(WindowShowHide)
4+
5+
set(CMAKE_CXX_STANDARD 17)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
8+
find_package(Qt6 REQUIRED COMPONENTS Core Widgets)
9+
if (Qt6_FOUND)
10+
qt_standard_project_setup()
11+
else ()
12+
find_package(Qt5 REQUIRED COMPONENTS Core Widgets)
13+
set(CMAKE_AUTOMOC ON)
14+
set(CMAKE_AUTORCC ON)
15+
set(CMAKE_AUTOUIC ON)
16+
endif ()
17+
18+
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE src/WindowShowHide.cpp src/WindowShowHide.h)
19+
target_link_libraries(${PROJECT_NAME} Qt::Core Qt::Widgets)
20+
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Qt.Widgets/Others")
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# WindowShowHide
2+
3+
Demonstrates the use of QMainWindow show, hide and close methods.
4+
5+
## Sources
6+
7+
[src/WindowShowHide.h](src/WindowShowHide.h)
8+
9+
[src/WindowShowHide.cpp](src/WindowShowHide.cpp)
10+
11+
[CMakeLists.txt](CMakeLists.txt)
12+
13+
## Output
14+
15+
![Screenshot](../../../docs/Pictures/WindowShowHide.png)
16+
17+
## Generate and build
18+
19+
### Qt Creator
20+
21+
To build these projects, open `WindowShowHide.pro` file with Qt Creator.
22+
23+
### CMake
24+
25+
To build this project, open "Terminal" and type following lines:
26+
27+
Set `CMAKE_PREFIX_PATH` with Qt6 install path.
28+
29+
#### Windows :
30+
31+
``` cmake
32+
mkdir build
33+
cd build
34+
cmake ..
35+
start ./WindowShowHide.sln
36+
```
37+
38+
#### macOS :
39+
40+
``` cmake
41+
mkdir build
42+
cd build
43+
cmake .. -G "Xcode"
44+
open ./WindowShowHide.xcodeproj
45+
```
46+
47+
#### Linux with Code::Blocks :
48+
49+
``` cmake
50+
mkdir build
51+
cd build
52+
cmake .. -G "CodeBlocks - Unix Makefiles"
53+
xdg-open ./WindowShowHide.cbp > /dev/null 2>&1
54+
```
55+
56+
#### Linux :
57+
58+
``` cmake
59+
mkdir build
60+
cd build
61+
cmake ..
62+
cmake --build . --config Debug
63+
./WindowShowHide
64+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG += c++17
2+
QT = widgets
3+
SOURCES = src/WindowShowHide.cpp
4+
HEADERS = src/WindowShowHide.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "WindowShowHide.h"
2+
3+
using namespace Examples;
4+
5+
int main(int argc, char *argv[]) {
6+
auto application = QApplication {argc, argv};
7+
auto window1 = Window1 {};
8+
window1.show();
9+
return application.exec();
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)