Initiating Boilerplate Design
I'm now focusing on the core requirement: a functioning C++ Qt application boilerplate. The central concern is providing a solid foundation with all essential elements, including file structure and build system setup, to launch a basic window. This will streamline the development process for future projects.
Mapping Project Structure
I've been mapping the key components, breaking down a typical Qt project into manageable chunks. This includes essential files like main.cpp, mainwindow.cpp, and .h files. I am now focusing on setting up a clean folder structure and mapping out CMakeLists.txt with essential directives such as cmake_minimum_required, project and finding Qt5/Qt6. I will provide guidance for linking libraries as well.
Constructing Core Dependencies
I'm now integrating crucial components for a functional Qt6 C++ boilerplate. I've included find_package(Qt6 REQUIRED COMPONENTS Widgets) and will incorporate qt_standard_project_setup(), qt_add_executable, and target_link_libraries directives in the CMakeLists.txt. I'm aiming for simplicity while ensuring essential functionality. For main.cpp, mainwindow.h, and mainwindow.cpp, I'll define basic structures, including a QMainWindow, setup, and destruction.
Optimizing Code Generation
I'm now refining the output, focusing on how the boilerplate will be constructed and built, along with the option for a code-only variant for users who prefer that approach. I've begun crafting the response structure, including sections on file layout and individual files like CMakeLists.txt, ensuring clarity and practicality. Explicit variables like CMAKE_CXX_STANDARD are used.
Finalizing UI Implementation
Now I'm finalizing the minimal UI XML for mainwindow.ui with a basic label. I'm also preparing standard build instructions using cmake. Furthermore, I've checked the design against best practices, focusing on Qt 6, CMake, and responsible memory management. Finally, I'm generating both "Standard GUI" and "Minimal Single-File" versions, along with an explicit mainwindow.ui section. The overall output is almost complete.
Improving Clarity for Users
I'm presently tackling a common point of confusion for beginners: the handling of the ui_mainwindow.h include. I'm focusing on ensuring its clear presentation within the boilerplate to avoid any initial hurdles. I'm also ensuring the namespace in the UI file aligns with the C++ code for seamless integration. I'm structuring the output in a clear way: Project Structure, CMakeLists.txt, Header File, Source File, Main File, UI file (XML), and Build Instructions.
Here is a complete, modern boilerplate for a C++ desktop application using Qt 6 and CMake.
This template uses the standard architecture: separating the class definition, implementation, main entry point, and the UI definition (XML).
1. Project Structure
Create a folder named MyQtApp and arrange the files like this:
MyQtApp/
├── CMakeLists.txt
├── src/
│ ├── main.cpp
│ ├── mainwindow.cpp
│ └── mainwindow.h
└── forms/
└── mainwindow.ui
2. The Files
CMakeLists.txt
This configures your build system. It sets up standard C++ settings and finds the Qt libraries.
cmake_minimum_required(VERSION 3.16)
project(MyQtApp VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Qt CMake settings
set(CMAKE_AUTOMOC ON) # Automatically run moc for Q_OBJECT
set(CMAKE_AUTORCC ON) # Automatically run rcc for resources
set(CMAKE_AUTOUIC ON) # Automatically run uic for .ui files
# Find Qt 6 libraries
find_package(Qt6 REQUIRED COMPONENTS Widgets)
# Define source files
set(PROJECT_SOURCES
src/main.cpp
src/mainwindow.cpp
src/mainwindow.h
forms/mainwindow.ui
)
# Create executable
add_executable(MyQtApp ${PROJECT_SOURCES})
# Link Qt libraries
target_link_libraries(MyQtApp PRIVATE Qt6::Widgets)
# Setup specific Window compilation settings (handles WinMain on Windows)
set_target_properties(MyQtApp PROPERTIES
WIN32_EXECUTABLE ON
MACOSX_BUNDLE ON
)
src/mainwindow.h
The header file for your main application window.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
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;
};
#endif // MAINWINDOW_H
src/mainwindow.cpp
The implementation logic. Note the include logic for ui_mainwindow.h (this file is auto-generated by CMake during the build).
#include "mainwindow.h"
#include "./ui_mainwindow.h" // This file is generated from the .ui file
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Example: Connect a button inside the UI to a lambda function
// connect(ui->myButton, &QPushButton::clicked, this, [](){ ... });
}
MainWindow::~MainWindow()
{
delete ui;
}
src/main.cpp
The entry point that initializes the event loop.
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
forms/mainwindow.ui
Normally you edit this file using Qt Designer, but here is the minimal XML required to make the project compile and show a blank window. Copy and paste this content.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>My Qt App</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
3. How to Build
You need a C++ compiler (GCC, Clang, or MSVC) and CMake installed.
Using the Command Line:
- Open your terminal in the project root (
MyQtApp/).
- Run the following commands:
mkdir build
cd build
cmake ..
cmake --build .
- Run the executable generated in the build folder.
Using Qt Creator (Recommended):
- Open Qt Creator.
- Go to File > Open File or Project.
- Select the
CMakeLists.txt file.
- Configure the Kit (select your installed Qt version).
- Press Run (Green Play arrow).
4. Code-Only Version (No .ui file)
If you prefer to code your UI manually without the .ui XML file / Designer, here is the simplified version:
CMakeLists.txt:
Remove src/mainwindow.ui from sources and set(CMAKE_AUTOUIC ON).
src/mainwindow.h:
Remove namespace Ui, the private Ui::MainWindow *ui, and the destructor.
src/mainwindow.cpp:
#include "mainwindow.h"
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
this->resize(800, 600);
// Create widgets manually
QPushButton *btn = new QPushButton("Hello World", this);
btn->setGeometry(10, 10, 100, 30);
}
MainWindow::~MainWindow() {}