Wednesday, December 28, 2022

Qt Widget hello world code only

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
#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
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})
view raw CMakeList.txt hosted with ❤ by GitHub

No comments:

Configuring TUN/TAP virtual network interface for use with QEMU on Xubuntu 24.04

Configuring TUN/TAP virtual network interface for use with QEMU on Xubuntu 24.04 I am planning to run qemu-system-ppc to play around QEMU ...