Creating C++ Qt Boilerplate
The user is looking for a boilerplate for a C++ Qt desktop application, but they haven’t specified the Qt version or build system. Typically, Qt Widgets with CMake is common in modern Qt6, but qmake is still in use. I need to select a default, perhaps give examples using both. I'll create a minimal app example in main.cpp, with a CMakeLists.txt, and a bit of structure without being overly verbose. This includes components like QApplication, QMainWindow, and menu setups.Preparing Qt Boilerplate Overview
I think targeting Qt 6 is a good idea since it’s the latest, but I can note that the setup is similar for Qt 5. I’ll mention how to adapt find_package and target_link_libraries for Qt5 if needed, keeping things generic since the user didn’t specify an OS. I’ll provide a clear directory layout and necessary files without adding too many extraneous details. Keeping it straightforward and focused is key. Below is a minimal boilerplate for a C++ Qt Widgets desktop application using CMake and Qt 6.
You’ll get:
CMakeLists.txt
main.cpp
mainwindow.h
mainwindow.cpp
mainwindow.ui (optional; for Designer-based UI)
You can omit the .ui file and do everything in code if you prefer.
Directory structure
MyQtApp/
├─ CMakeLists.txt
├─ main.cpp
├─ mainwindow.h
├─ mainwindow.cpp
└─ mainwindow.ui (optional, if using Qt Designer)
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
# Project name and language
project(MyQtApp LANGUAGES CXX)
# Use modern C++
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Find Qt (Qt6; for Qt5 see notes below)
find_package(Qt6 REQUIRED COMPONENTS Widgets)
# For Qt5, use:
# find_package(Qt5 REQUIRED COMPONENTS Widgets)
# Enable automoc, autouic, autorcc to simplify build setup
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
add_executable(MyQtApp
main.cpp
mainwindow.h
mainwindow.cpp
mainwindow.ui # remove this if you don't use a .ui file
)
# Link against Qt
target_link_libraries(MyQtApp
PRIVATE
Qt6::Widgets
)
# For Qt5, use:
# target_link_libraries(MyQtApp
# PRIVATE
# Qt5::Widgets
# )
# On Windows, make sure the console window does not appear for GUI apps (optional)
if(WIN32)
set_target_properties(MyQtApp PROPERTIES WIN32_EXECUTABLE TRUE)
endif()
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
mainwindow.h
#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:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Example: set window title
setWindowTitle("MyQtApp");
// You can connect signals/slots here, e.g.:
// connect(ui->myButton, &QPushButton::clicked, this, &MainWindow::onMyButtonClicked);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.ui (optional, from Qt Designer)
If you use Qt Designer, create a new “Main Window” form and save it as mainwindow.ui in the same directory.
A minimal XML version (you can auto-generate with Designer instead of writing this):
<?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>MyQtApp</string>
</property>
<widget class="QWidget" name="centralwidget">
<!-- Add your central widgets/layouts here -->
</widget>
<widget class="QMenuBar" name="menubar"/>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Building
From the MyQtApp directory:
mkdir build
cd build
cmake .. # Optionally: cmake -DCMAKE_PREFIX_PATH=/path/to/Qt ..
cmake --build .
Run the resulting executable (MyQtApp, or MyQtApp.exe on Windows) from build/.
If you want a boilerplate using QML/Qt Quick instead, or using qmake instead of CMake, say which combination you prefer and I’ll adapt the template.