From 7ee1669522bdb6c2c6dae6a0ad7f9a9a60f87ff5 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 4 Apr 2025 16:00:37 +0200 Subject: Initial commit --- .gitignore | 3 +++ Makefile | 26 +++++++++++++++++++ README.md | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 11 ++++++++ mainwindow.cpp | 13 ++++++++++ mainwindow.h | 19 ++++++++++++++ mainwindow.ui | 21 +++++++++++++++ 7 files changed, 173 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 mainwindow.ui diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..872afed --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +moc_* +ui_* +run diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4ba3ce9 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +QTINCPATH = /usr/include/qt6 +QTTOOLS = /usr/lib64/qt6/libexec + +CC = g++ +MOC = ${QTTOOLS}/moc +UIC = ${QTTOOLS}/uic +INCLUDE = -I ${QTINCPATH} \ + -I ${QTINCPATH}/QtWidgets +QTLIB = -lQt6Widgets \ + -lQt6Core \ + -lQt6Gui +SRC = main.cpp mainwindow.cpp moc_mainwindow.cpp + +all: moc ui + ${CC} ${SRC} ${INCLUDE} ${QTLIB} -o run + +moc: + ${MOC} mainwindow.h > moc_mainwindow.cpp + +ui: + ${UIC} mainwindow.ui > ui_mainwindow.h + +clean: + rm moc_* ui_* run + +.PHONY: all moc ui clean diff --git a/README.md b/README.md new file mode 100644 index 0000000..a56ea9e --- /dev/null +++ b/README.md @@ -0,0 +1,80 @@ +# A minimal QT application, without the nonsense + +This repository contains a minimal QT application that can be compiled +without using CMake or QT Creator. + +## Requirements + +* A C++ compiler (tested with GCC 14.2.1 and Clang 19.1.7, both on Linux-x86_64) +* QT libraries and headers +* The QT development tools `moc` and `uic` + +On Fedora Linux they are contained in the `qt6-qtbase-devel`. I have +not tested this on other operating systems. + +## tl;dr + +If you just want to try out this program you can run (if you have `make` installed) + +``` +$ make && ./run +``` + +## Explanation + +Following the most basic QT Creator template, there are 4 source code files: + +* `main.cpp`: the main source file +* `mainwindow.h`: the header file for the main window of the application +* `mainwindow.cpp`: the C++ source for the main window +* `mainwindow.ui`: the UI file with an XML description of the main window + +The compilation is done in 3 steps: + +1. Pre-process `mainwindow.h` with `moc` to obtain `moc_mainwindow.cpp` +2. Compile `mainwindow.ui` with `uic` to obtain `ui_mainwindow.h` +3. Compile the C++ files, including `moc_mainwindow.cpp` + +The `Makefile` implements these three steps. + +The first two steps are straightforward: first you need to locate the +`moc` and `uic` command - they are usually in the QT installation +folder. For example I have them in `/usr/lib64/qt/libexec`. Then run: + +``` +$ /usr/lib64/qt/libexec/moc mainwindow.h > moc_mainwindow.cpp +$ /usr/lib64/qt/libexex/uic mainwindow.ui > ui_mainwindow.h +``` + +These steps can be skipped if one prefers to write the `moc_` and `ui_` +files directly, removing the dependency on the two tools. + +For the last step, one needs to include the header files and link with +the QT libraries. + +On my system the header files are in `/usr/include/qt6`. This means I +need to add the option `-I /usr/include/qt6` to my `gcc` command. This +simple project also requires headers contained in a subfolder of this, +so I will add a second `-I` option (see the full command below). + +As for the system libraries, in my case they are installed in a system +folder that is scanned by default by the linker, so I don't have to +specify the path. We need the files `libQt6Widgets.so`, `libQt6Core.so` +and `libQt6Gui.so`, which we can include with +`-lQt6Widgets -lQt6Core -lQt6Gui`. If your linker does not find them, +locate them and include their folder with `-L /path/to/the/folder`. + +So the command for step 3 becomes: + +``` +$ g++ main.cpp mainwindow.cpp moc_mainwindow.cpp \ + -I /usr/include/qt6 -I /usr/include/qt6/QtWidgets \ + -lQt6Widgets -lQt6Core -lQt6Gui + -o run +``` + +And finally you can enjoy your new app: + +``` +$ ./run +``` diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..1cd93da --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..ebaa88c --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,13 @@ +#include "mainwindow.h" +#include "./ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent), ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..ef1225e --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,19 @@ +#include + +QT_BEGIN_NAMESPACE +namespace Ui { +class MainWindow; +} +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..f9481ec --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,21 @@ + + +MainWindow + + + + + 0 + 0 + 800 + 600 + + + + My first QT app, without the nonsense! + + + + + + -- cgit v1.3