Skip to content

Commit b24358a

Browse files
authored
Merge pull request #8 from XuhuaHuang/Qt_console_app
Uploaded preliminary Qt console app with CMake
2 parents a87b530 + 4acf280 commit b24358a

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

Qt/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*_autogen/

Qt/Console/CMakelists.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Qt itself requires at least CMake version 3.16
2+
# Qt version 5.14 is required for this project
3+
cmake_minimum_required(VERSION 3.20)
4+
5+
# ┌──────────────────────────────────────────────────────────────────┐
6+
# │ Projects Settings │
7+
# └──────────────────────────────────────────────────────────────────┘
8+
project(ConsoleApplication
9+
VERSION 1.0.0
10+
DESCRIPTION "Qt console application with CMake example"
11+
LANGUAGES CXX
12+
)
13+
14+
set(CMAKE_CXX_STANDARD 17)
15+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
16+
17+
# ┌──────────────────────────────────────────────────────────────────┐
18+
# │ Qt │
19+
# └──────────────────────────────────────────────────────────────────┘
20+
set(CMAKE_AUTOMOC ON)
21+
# set(CMAKE_AUTORCC ON)
22+
# set(CMAKE_AUTOUIC ON)
23+
24+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
25+
26+
if(WIN32)
27+
set(Qt5_DIR "C:/Qt/Qt5.14.0/5.14.0/msvc2017_64/lib/cmake/Qt5" CACHE PATH "directory where Qt5Config.cmake exists.")
28+
elseif(APPLE)
29+
set(Qt5_DIR "/usr/local/Cellar/qt/5.14.0/clang_64/lib/cmake/Qt5/" CACHE PATH "directory where Qt5Config.cmake exists.")
30+
else()
31+
set(Qt5_DIR "" CACHE PATH "directory where Qt5Config.cmake exists.")
32+
endif()
33+
34+
find_package(Qt5 REQUIRED COMPONENTS Widgets)
35+
36+
if(WIN32)
37+
set(CMAKE_WIN32_EXECUTABLE ON)
38+
elseif(APPLE)
39+
set(CMAKE_MACOSX_BUNDLE ON)
40+
endif()
41+
42+
# ┌──────────────────────────────────────────────────────────────────┐
43+
# │ Executable │
44+
# └──────────────────────────────────────────────────────────────────┘
45+
# set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/build")
46+
add_executable(ConsoleApplication
47+
# "mainwindow.ui"
48+
# "mainwindow.cpp"
49+
"main.cpp"
50+
"resources.qrc"
51+
)
52+
53+
# Note that the application will still link against Qt5::Core
54+
# since Qt5::Widgets depends on it.
55+
target_link_libraries(ConsoleApplication PRIVATE Qt5::Core)
56+
57+
set_target_properties(ConsoleApplication PROPERTIES
58+
WIN32_EXECUTABLE ON
59+
MACOSX_BUNDLE OFF
60+
)

Qt/Console/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### Console
2+
3+
This projects uses `Qt` 5.14 (`Qt5`) on Windows with `MSVC17` compiler.<br>
4+
5+
#### A few things
6+
1. The default installation location of `Qt` is `C:\Qt\Qt5.14.0`.<br>
7+
2. Following the default installation option, make sure to add `C:\Qt\Qt5.14.0\5.14.0\msvc2017_64\bin` to the system `path` variable.<br>
8+
3. The `Qt5Config.cmake` file required by `CMakelists.txt` `find_package()` is located in `C:\Qt\Qt5.14.0\5.14.0\msvc2017_64\lib\cmake\Qt5`
9+
10+
#### Build
11+
```shell
12+
# change to the directory where this file could be found
13+
mkdir build
14+
cd build
15+
cmake ..
16+
start ./ConsoleApplication.sln
17+
```
18+
19+
#### References
20+
> https://github.com/ArthurSonzogni/cmake-Qt5-example<br>
21+
> https://github.com/miurahr/cmake-qt-packaging-example

Qt/Console/main.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*****************************************************************//**
2+
* \file main.cpp
3+
* \brief Qt console application with CMake example
4+
*
5+
* \author Xuhua Huang
6+
* \date January 31, 2021
7+
*********************************************************************/
8+
9+
#include <iostream>
10+
11+
#include <QTextStream>
12+
#include <QString>
13+
#include <QDebug>
14+
#include <QList>
15+
16+
int main(void)
17+
{
18+
// output with <iostream>
19+
std::cout << "Hello Qt console" << "\n";
20+
21+
// QString, I/O stream
22+
QTextStream out(stdout); // output stream object
23+
QTextStream in(stdin); // input stream object
24+
25+
out << "This is a console application" << "\n";
26+
out.flush();
27+
out << "Please enter your name" << "\n";
28+
out.flush();
29+
30+
// fixing the issue to enter full name with a space
31+
std::string tempName; // temporary variable
32+
std::getline(std::cin, tempName); // use getline() function with std::istream
33+
34+
// convert std::string to QString and print to console
35+
// with static factory function fromStdString
36+
QString Name = QString::fromStdString(tempName); // convert
37+
out << "Hello " << Name << "!" << "\n"; // print
38+
out.flush();
39+
40+
// use qDebug() to print to console
41+
qDebug() << "Using qDebug() to send debugging messages to console in Desktop apps" << "\n";
42+
43+
// QString, append(), prepend()
44+
QString a = "is";
45+
a.append(" awesome");
46+
a.prepend("Music ");
47+
48+
out << "Original mesasge: " << a << "\n";
49+
out << "The a string has " << a.count()
50+
<< " characters" << "\n";
51+
52+
// toUpper() and toLower()
53+
out << a.toUpper() << "\n";
54+
out << a.toLower() << "\n";
55+
56+
// QList
57+
QList<int> integerList;
58+
QList<QString> stringList = QList<QString>() << "Canada" << "U.S.A" << "Mexico";
59+
for (int i = 0; i < 6; i++)
60+
integerList.append(i);
61+
integerList.removeOne(4);
62+
63+
// output the integer integerList
64+
foreach(int n, integerList)
65+
{
66+
qDebug() << n;
67+
}
68+
69+
// output the string in stringList
70+
foreach(QString str, stringList)
71+
{
72+
qDebug() << str;
73+
}
74+
75+
return EXIT_SUCCESS;
76+
}

Qt/Console/resources.qrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE RCC><RCC version="1.0">
2+
<qresource>
3+
<file>"./README.md"</file>
4+
</qresource>
5+
</RCC>

0 commit comments

Comments
 (0)