Qt Widget hello world code only
This post shows how to create a Hello World Qt Widget application in pure code, not using the UI Designer.▣ Create a console application - consult this link for a quick guide.
▣ Change main.cpp like below
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QLabel> | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
QLabel* label = new QLabel("Hello world", 0); | |
label->resize(300,40); | |
label->show(); | |
return a.exec(); | |
} |
▣ Update CMakeList.txt like below
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.14) | |
project(HelloWorldWidgetCode LANGUAGES CXX) | |
set(CMAKE_AUTOUIC ON) | |
set(CMAKE_AUTOMOC ON) | |
set(CMAKE_AUTORCC ON) | |
set(CMAKE_CXX_STANDARD 17) | |
set(CMAKE_CXX_STANDARD_REQUIRED ON) | |
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) | |
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) | |
add_executable(HelloWorldWidgetCode | |
main.cpp | |
) | |
target_link_libraries(HelloWorldWidgetCode Qt${QT_VERSION_MAJOR}::Widgets) | |
install(TARGETS HelloWorldWidgetCode | |
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) |
No comments:
Post a Comment