How to build a simple C++ QT6 program.

A cmake configuration file "CMakeLists.txt" :
Code:
cmake_minimum_required(VERSION 3.16)
project(LabelExample VERSION 1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find the Qt 6 Widgets module
find_package(Qt6 REQUIRED COMPONENTS Widgets)

qt_add_executable(LabelExample main.cpp)

# Link against the Qt 6 Widgets library
target_link_libraries(LabelExample PRIVATE Qt6::Widgets)

# Recommended: specific to a target
target_include_directories(LabelExample PUBLIC /usr/local/include)

# Alternative: global for all targets in this directory
include_directories(/usr/local/include)

The application "main.cpp" :
Code:
#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // Create a label with your desired text
    QLabel label("Hello from Qt 6!");
    
    // Optional: resize the label window
    label.resize(300, 100);
    label.setAlignment(Qt::AlignCenter);
    
    label.show();

    return app.exec();
}

A shell script to build & run :
Code:
rm CMakeCache.txt
rm -rf CMakeCache.txt CMakeFiles/
rm Makefile
rm -vfR ./build
mkdir   ./build
cd      ./build
# specify source and target directory
cmake -S .. -B ./build/ ..
cmake -DCMAKE_CXX_COMPILER=clang++20 -DCMAKE_C_COMPILER=clang20 ..
cmake --build .
./LabelExample
 
Something more complicated a button with a callback function.
Qt uses signals and slots (handlers).
Explanation follows later,

cat CMakeLists.txt
Code:
cmake_minimum_required(VERSION 3.16)
project(LabelExample VERSION 1.0 LANGUAGES C CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 REQUIRED COMPONENTS Widgets)
qt_add_executable(LabelExample main.cpp mainwindow.cpp mainwindow.h)
target_include_directories(LabelExample PUBLIC /usr/local/include )
target_link_libraries(LabelExample PRIVATE Qt6::Widgets)

cat main.cpp
Code:
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
}

cat mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>

class MainWindow : public QMainWindow {
    Q_OBJECT // Required for signals and slots to work

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    // This is your callback function (the "slot")
    void handleButtonClicked(); 

private:
    QPushButton *m_button;
};

#endif // MAINWINDOW_H

cat mainwindow.cpp
Code:
#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    this->setWindowTitle("My Window");
    // Create the button
    m_button = new QPushButton("Click Me", this);
    setCentralWidget(m_button);

    // Connect: (Sender, Signal, Receiver, Slot)
    // No lambda is used here; we point directly to the member function.
    connect(m_button, &QPushButton::clicked, this, &MainWindow::handleButtonClicked);
}

MainWindow::~MainWindow(){};

// Definition of the callback
void MainWindow::handleButtonClicked() {
    qDebug() << "Button was clicked!";
}
 
Thank you for this! I'm trying to get back to programming, now that I'm happily retired. I'm migrating everything from Linux and MacOS to FreeBSD, and trying to relearn modern C++ and Ada, which are the 2 (favorite) languages I used a long time ago along with LISP. Will also try Julia. Took me a long time to get my emacs setup the way I think I want it. If you don't mind my asking, before trying the above code, I guess I would just need to install devel/qt6-base, correct? If possible, would like to avoid installing the full QTCreator IDE.

I don't think Ada is that popular now, but would you happen to know if the alire tool works well on FreeBSD? I'm successfully using eglot+clangd for C++, but I'm clueless on getting the Ada LSP server running on FreeBSD (I did get it to work on MacOS, though).

Thanks again for this. Very much appreciated.
 
Ada is a nice language, but for binding with external API's you need to write wrappers for all functions which too much of a pain. So i dropped ADA for this reason. Two languages which are also interesting are NIM & Dlang
 
I'm a regular contributor on the Qt corporate forums under the same handle as here. I've been programming it since Qt3. I love the platform, but I have no use for QML and instead prefer doing everything in C++ using the traditional Makefile model instead of using cmake.
 
What is the problem with QML.
So configuration in one file, then import in another file.
The problem is you end split.
One thing here, one thing other place.
My brain works differently, i have an idea , one place , the code ,no need for external configs (even xml).
I once tried kotlin , you are forced , to use an ide, it links and does stuff behind, then one problem ?, one does not know. It's automatic.
 
Ada is a nice language, but for binding with external API's you need to write wrappers for all functions which too much of a pain. So i dropped ADA for this reason. Two languages which are also interesting are NIM & Dlang
Thanks for your comments. What I did a looooong, long time ago, was write kind of a transaction dispatcher that would listen on TCP/IP sockets.... This was a banking system. That dispatcher refused to die for 20+ yrs
:)
 
Sorry have lots of space but dont now,
pkg info | grep qt6
Code:
lazarus-qt6-4.4_1              Portable Delphi-like IDE for the FreePascal compiler (Qt6)
libqaccessibilityclient-qt6-0.6.0 Helper to make writing accessibility tools easier
libqt6xdg-4.3.0_1              Qt6 implementation of freedesktop.org xdg specs
phonon-qt6-4.12.0_3            KDE multimedia framework
polkit-qt-1-qt6-0.200.0        Qt wrapper around Polkit libraries
qca-qt6-2.3.10_2               Cross-platform crypto API for Qt 6
qt6-5compat-6.10.1             Qt 6 module that contains unsupported Qt 5 APIs
qt6-base-6.10.1_1              Qt base (core, gui, widgets, network,...)
qt6-base_sqldriver-sqlite-6.10.1 Qt base (sqlite sqldriver)
qt6-charts-6.10.1              Qt 6 charts module
qt6-declarative-6.10.1_2       Qt declarative framework for dynamic user interfaces
qt6-multimedia-6.10.1_1        Qt audio, video, radio and camera support module
qt6-positioning-6.10.1         Qt 6 API to provide positioning information via QML and C++
qt6-quick3d-6.10.1             Provides a high-level API for creating 3D content or UIs based on Qt Quick
qt6-quicktimeline-6.10.1       Enables keyframe-based animations and parameterization.
qt6-scxml-6.10.1               Qt6 SCXML module
qt6-sensors-6.10.1             Qt sensors module
qt6-serialport-6.10.1          Qt functions to access serial ports
qt6-shadertools-6.10.1         Provides tools for the cross-platform Qt shader pipeline
qt6-speech-6.10.1              Accessibilty features for Qt6
qt6-svg-6.10.1                 Qt6 classes for displaying the contents of SVG files
qt6-tools-6.10.1               Qt declarative framework for dynamic user interfaces
qt6-translations-6.10.1        Qt 6 translation module
qt6-wayland-6.10.1             Qt6 wrapper for Wayland
qt6pas-6.2.10_1                Qt6 binding for FreePascal
qtkeychain-qt6-0.15.0          Platform independent Qt API for storing passwords
 
I would install devel/qtcreator too which is the best IDE when dealing with QT. I use it at work to develop cross-platform QT6 apps. Highly can recommend.
Thanks. I just arrived to the same conclusion after trying to do QT development using emacs. However, it would seem that if in the future I need to do any commercial, closed source, software development, I would need to pay for a QT license.
 
Note,
You code can be private ...

You can use the Qt Community Edition at no cost if you follow these obligations:

  • LGPL Compliance: Most of Qt is under the LGPL (Lesser General Public License). You can build closed-source commercial apps for free as long as you:
    • Link Dynamically: Use shared libraries (.dll or .so files) so users can replace the Qt version themselves.
    • Provide Source Code for Qt: You must provide the source code of the Qt library itself (not your own app code) upon request.
    • Acknowledge Qt: Explicitly state that your software uses the Qt framework
 
Back
Top