diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-04 16:21:22 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-04 16:21:22 +0200 |
| commit | 7219836b03f169f9ea69e4dfdf3f35dfdba80ab0 (patch) | |
| tree | 782ef1a9d55d20fa03de55562b958e5f3f497e71 /src/blog | |
| parent | 111ca020e8a8bc480663afb62f2006e35b732320 (diff) | |
| download | sebastiano.tronto.net-7219836b03f169f9ea69e4dfdf3f35dfdba80ab0.tar.gz sebastiano.tronto.net-7219836b03f169f9ea69e4dfdf3f35dfdba80ab0.zip | |
Added blog post
Diffstat (limited to 'src/blog')
| -rw-r--r-- | src/blog/2025-04-04-qt-minimal/qt-minimal.md | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/blog/2025-04-04-qt-minimal/qt-minimal.md b/src/blog/2025-04-04-qt-minimal/qt-minimal.md new file mode 100644 index 0000000..6f112bb --- /dev/null +++ b/src/blog/2025-04-04-qt-minimal/qt-minimal.md | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | # Getting started with QT, without the nonsense | ||
| 2 | |||
| 3 | At work I have recently (yesterday) ended working on a big project and | ||
| 4 | I am now waiting for a new assignment. While I wait, I decided that I | ||
| 5 | should take some time to learn something that could be useful both at | ||
| 6 | work and for my personal projects. After some thought, I settled on QT | ||
| 7 | development with C++. | ||
| 8 | |||
| 9 | Any tutorial on this topic tells you to start by downloading QT Creator, | ||
| 10 | an full-blown IDE, and set up a new project from there. In a few clicks | ||
| 11 | you'll have a couple of source code files and a CMakeLists.txt full of | ||
| 12 | [CMake](https://nl.wikipedia.org/wiki/CMake) magic. Ready to go! | ||
| 13 | |||
| 14 | But there is a problem. I don't like full-blown IDEs (although admittedly | ||
| 15 | QT Creator seems quite snappy), and I don't like being locked into specific | ||
| 16 | tools such as CMake. I tried to look online how to set up a QT project | ||
| 17 | without these, but I could not find anything, so I had to figure it out on | ||
| 18 | my own. And then I wrote this post so you don't have have to waste your | ||
| 19 | time like I did :) | ||
| 20 | |||
| 21 | You can find the source code for this tiny project | ||
| 22 | [here](https://git.tronto.net/minimal-qt) (or | ||
| 23 | [here](https://github.com/sebastianotronto/minimal-qt)) if you have bad | ||
| 24 | tase and prefer github over my [self-hosted](../2022-11-23-git-host/) | ||
| 25 | git instance. If you just want to try it out you can run, from the | ||
| 26 | project's main folder: | ||
| 27 | |||
| 28 | ``` | ||
| 29 | make && run | ||
| 30 | ``` | ||
| 31 | |||
| 32 | But keep reading if you want to understand how this works! | ||
| 33 | |||
| 34 | ## Requirements | ||
| 35 | |||
| 36 | * A C++ compiler (tested with GCC 14.2.1 and Clang 19.1.7, both on Linux-x86_64) | ||
| 37 | * QT libraries and headers | ||
| 38 | * The QT development tools `moc` and `uic` | ||
| 39 | |||
| 40 | On Fedora Linux they are contained in the `qt6-qtbase-devel` package. | ||
| 41 | I have not tested this on other operating systems. | ||
| 42 | |||
| 43 | ## Explanation | ||
| 44 | |||
| 45 | Following the most basic QT Creator template, there are 4 source code files: | ||
| 46 | |||
| 47 | * `main.cpp`: the main source file | ||
| 48 | * `mainwindow.h`: the header file for the main window of the application | ||
| 49 | * `mainwindow.cpp`: the C++ source for the main window | ||
| 50 | * `mainwindow.ui`: the UI file with an XML description of the main window | ||
| 51 | |||
| 52 | The compilation is done in 3 steps: | ||
| 53 | |||
| 54 | 1. Pre-process `mainwindow.h` with `moc` to obtain `moc_mainwindow.cpp` | ||
| 55 | 2. Compile `mainwindow.ui` with `uic` to obtain `ui_mainwindow.h` | ||
| 56 | 3. Compile the C++ files, including `moc_mainwindow.cpp` | ||
| 57 | |||
| 58 | The `Makefile` implements these three steps. | ||
| 59 | |||
| 60 | The first two steps are straightforward: first you need to locate the | ||
| 61 | `moc` and `uic` commands. They are usually in the QT installation | ||
| 62 | folder - for example I have them in `/usr/lib64/qt/libexec`. Then run: | ||
| 63 | |||
| 64 | ``` | ||
| 65 | /usr/lib64/qt/libexec/moc mainwindow.h > moc_mainwindow.cpp | ||
| 66 | /usr/lib64/qt/libexex/uic mainwindow.ui > ui_mainwindow.h | ||
| 67 | ``` | ||
| 68 | |||
| 69 | These steps can be skipped if one prefers to write the `moc_` and `ui_` | ||
| 70 | files directly, removing the dependency on the two tools. | ||
| 71 | |||
| 72 | For the last step, one needs to include the header files and link with | ||
| 73 | the QT libraries. | ||
| 74 | |||
| 75 | On my system the header files are in `/usr/include/qt6`. This means I | ||
| 76 | need to add the option `-I /usr/include/qt6` to my `g++` command. This | ||
| 77 | project also requires headers contained in a subfolder of this, | ||
| 78 | so I will add a second `-I` option (see the full command below). | ||
| 79 | |||
| 80 | As for the shared libraries, in my case they are installed in a system | ||
| 81 | folder that is scanned by default by the linker, so I don't have to | ||
| 82 | specify the path. We need the files `libQt6Widgets.so`, `libQt6Core.so` | ||
| 83 | and `libQt6Gui.so`, which we can include with | ||
| 84 | `-lQt6Widgets -lQt6Core -lQt6Gui`. If your linker does not find them, | ||
| 85 | locate them and include their folder with `-L /path/to/the/folder`. | ||
| 86 | |||
| 87 | So the command for step 3 becomes: | ||
| 88 | |||
| 89 | ``` | ||
| 90 | g++ main.cpp mainwindow.cpp moc_mainwindow.cpp \ | ||
| 91 | -I /usr/include/qt6 -I /usr/include/qt6/QtWidgets \ | ||
| 92 | -lQt6Widgets -lQt6Core -lQt6Gui | ||
| 93 | -o run | ||
| 94 | ``` | ||
| 95 | |||
| 96 | And finally you can enjoy your new app: | ||
| 97 | |||
| 98 | ``` | ||
| 99 | ./run | ||
| 100 | ``` | ||
