The user wants a boilerplate for a C++ Qt desktop application.
1. Identify the Core Components:
To create a complete and useful boilerplate, I need more than just a main.cpp. A real-world application structure usually involves:
- Project Files:
CMakeLists.txt (modern standard) or .pro (qmake - older but still common). I should prefer CMake as it's the industry standard now.
- Source Structure: Separation of headers and source files.
- Main Entry Point:
main.cpp.
- Main Window Class:
MainWindow (inheriting from QMainWindow).
- Resource Management: A resource file (
.qrc) for icons/images.
- Configuration: An
Application class or singleton to handle global state/settings (optional but good for "boilerplate").
- README: Instructions on how to build it.
2. Determine the Scope/Complexity:
- Minimal: Just
main.cpp with a label. (Too simple).
- Standard:
CMakeLists.txt, main.cpp, MainWindow class, basic menu bar, status bar. (Just right).
- Over-engineered: Including MVVM patterns, complex factories, etc. (Too confusing for a starter).
3. Drafting the Content:
4. Refining the Output:
I should present this as a step-by-step guide or a ready-to-copy file structure.
- Option 1: Single code block. (Hard to distinguish files).
- Option 2: Separate code blocks for each file. (Clearer).
5. Add "Pro Tips" / Modern Context:
- Mention Qt Creator vs VS Code / CLion.
- Mention Qt6 vs Qt5. (I will aim for Qt6 but keep it compatible with Qt5 where possible, usually just
find_package syntax differs slightly).
- Mention folder structure best practices.
6. Final Code Polish (Mental Check):
- CMake:
find_package(Qt6 REQUIRED COMPONENTS Widgets) -> target_link_libraries(... Qt6::Widgets)
- C++:
Q_OBJECT in the class.
7. Structuring the response:
- Introduction: Brief overview.
- File Structure: Visual tree.
- Code Files:
CMakeLists.txt
src/main.cpp
src/MainWindow.h
src/MainWindow.cpp
- Build Instructions: How to run it.
Let's write the actual code.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(MyQtApp)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt6 REQUIRED COMPONENTS Widgets)
add_executable(MyQtApp
src/main.cpp
src/MainWindow.cpp
src/MainWindow.h
)
target_link_libraries(MyQtApp PRIVATE Qt6::Widgets)
MainWindow.h:
Standard boilerplate. Constructor, destructor, Q_OBJECT.
MainWindow.cpp:
Setup UI. Set central widget (maybe a label saying "Hello World"). Setup menus.
main.cpp:
Standard boilerplate.
Conclusion: The plan is solid. I will present the files clearly and include build commands for Linux/macOS/Windows.
Here is a clean, modern, and minimal boilerplate for a Qt 6 desktop application using CMake.
This structure separates the declaration (.h) from the implementation (.cpp) and sets up a basic Main Window.
Project Structure
MyQtApp/
├── CMakeLists.txt
├── src/
│ ├── main.cpp
│ ├── MainWindow.h
│ └── MainWindow.cpp
└── resources/
└── (icons, images, etc.)
1. Build Configuration (CMakeLists.txt)
This file tells the compiler where to find Qt and which source files to compile.
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)
# Automatically handle MOC, UIC, and RCC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
# Find the Qt6 package.
# You may need to set CMAKE_PREFIX_PATH to your Qt installation directory.
find_package(Qt6 REQUIRED COMPONENTS Widgets)
# Add source files
add_executable(MyQtApp
src/main.cpp
src/MainWindow.cpp
src/MainWindow.h
)
# Link Qt libraries to your executable
target_link_libraries(MyQtApp PRIVATE Qt6::Widgets)
# --- Windows Specific: Hide Console Window ---
if(WIN32)
set_target_properties(MyQtApp PROPERTIES
WIN32_EXECUTABLE TRUE
)
endif()
2. Main Window Header (src/MainWindow.h)
This defines the class structure. We use Q_OBJECT to support Qt's signal/slot mechanism.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenuBar>
#include <QStatusBar>
#include <QLabel>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
private slots:
void onActionExit();
void onActionAbout();
private:
void setupMenuBar();
void setupStatusBar();
// Example member widget
QLabel *m_statusLabel;
};
#endif // MAINWINDOW_H
3. Main Window Implementation (src/MainWindow.cpp)
This implements the logic, creates the menu bar, and sets up the layout.
#include "MainWindow.h"
#include <QMessageBox>
#include <QApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), m_statusLabel(new QLabel("Ready"))
{
setWindowTitle("My Qt Application");
resize(800, 600);
setupMenuBar();
setupStatusBar();
// Example: Setting a central widget (the main content area)
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
// You would typically set a layout on centralWidget here
}
MainWindow::~MainWindow() {
// Qt handles deletion of child widgets automatically,
// but raw pointers should be managed carefully.
}
void MainWindow::setupMenuBar() {
// File Menu
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
QAction *exitAction = fileMenu->addAction(tr("E&xit"));
exitAction->setShortcut(QKeySequence::Quit);
connect(exitAction, &QAction::triggered, this, &MainWindow::onActionExit);
// Help Menu
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
QAction *aboutAction = helpMenu->addAction(tr("&About"));
connect(aboutAction, &QAction::triggered, this, &MainWindow::onActionAbout);
}
void MainWindow::setupStatusBar() {
statusBar()->addWidget(m_statusLabel);
}
void MainWindow::onActionExit() {
QApplication::quit();
}
void MainWindow::onActionAbout() {
QMessageBox::information(this, tr("About"),
tr("This is a Qt 6 Desktop Boilerplate Application."));
}
4. Entry Point (src/main.cpp)
This is the starting point of the application.
#include <QApplication>
#include "MainWindow.h"
int main(int argc, char *argv[]) {
// Create the application object
QApplication app(argc, argv);
// Set application-wide info (useful for settings paths)
app.setOrganizationName("MyCompany");
app.setApplicationName("MyQtApp");
// Create and show the main window
MainWindow mainWindow;
mainWindow.show();
// Enter the main event loop
return app.exec();
}
How to Build and Run
Assuming you have Qt 6 installed and a C++ compiler (MSVC, GCC, or Clang):
- Create the folder structure as shown above.
- Configure (Generate Makefiles):
- Linux/Mac:
cmake -S . -B build
- Windows (MSVC): Open the folder in Qt Creator or run
cmake -B build -G "Visual Studio 17 2022" (adjust for your VS version).
- Compile:
- Run:
- Execute the binary located inside the
build folder (e.g., build/MyQtApp or build/Debug/MyQtApp.exe).
Recommended IDE Setup
- Qt Creator: The easiest option. Open the
CMakeLists.txt file directly via File > Open File or Project.
- Visual Studio Code: Install the "CMake Tools" and "Qt tools" extensions. You will likely need to set
CMAKE_PREFIX_PATH in your kit settings to point to your Qt version (e.g., C:/Qt/6.6.0/msvc2019_64).