Qt Project Structure & CMake

Understanding Qt project layout, CMakeLists.txt with Qt6, qmake vs CMake, and build configurations.

2 min read
16231 chars

Qt Project Layout

A typical Qt 6 CMake project looks like this:

MyApp/
├── CMakeLists.txt          # Top-level build script
├── main.cpp                # Entry point
├── mainwindow.h            # Main window header
├── mainwindow.cpp          # Main window implementation
├── mainwindow.ui           # Qt Designer UI file (optional)
├── resources.qrc           # Qt resource file (optional)
└── build/                  # Out-of-source build directory

For larger projects, split into subdirectories:

MyApp/
├── CMakeLists.txt
├── src/
│   ├── CMakeLists.txt
│   ├── main.cpp
│   └── core/
│       ├── engine.h
│       └── engine.cpp
├── ui/
│   └── mainwindow.ui
└── resources/
    ├── resources.qrc
    └── icons/

CMakeLists.txt — Full Qt6 Example

cmake_minimum_required(VERSION 3.16)

project(MyApp
    VERSION 1.0.0
    DESCRIPTION "My Qt Application"
    LANGUAGES CXX
)

# Require C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Qt auto-tools — must be set before find_package
set(CMAKE_AUTOMOC ON)   # Meta-Object Compiler
set(CMAKE_AUTORCC ON)   # Resource Compiler
set(CMAKE_AUTOUIC ON)   # UI Compiler

# Find Qt6 components
find_package(Qt6 REQUIRED COMPONENTS
    Core
    Widgets
    Network
    Quick
)

qt_standard_project_setup()

# Collect sources
set(SOURCES
    src/main.cpp
    src/mainwindow.cpp
)

set(HEADERS
    src/mainwindow.h
)

set(UI_FILES
    ui/mainwindow.ui
)

set(RESOURCES
    resources/resources.qrc
)

# Create executable
qt_add_executable(MyApp
    ${SOURCES}
    ${HEADERS}
    ${UI_FILES}
    ${RESOURCES}
)

# Link Qt modules
target_link_libraries(MyApp PRIVATE
    Qt6::Core
    Qt6::Widgets
    Qt6::Network
)

# Include dirs
target_include_directories(MyApp PRIVATE src/)

qmake vs CMake

Qt 6 recommends CMake for all new projects. qmake is still supported but considered legacy.

FeatureCMakeqmake
Qt recommendationPreferred (Qt 6+)Legacy
IDE supportQt Creator, CLion, VSQt Creator
Cross-platformExcellentGood
EcosystemBroad (vcpkg, Conan)Qt-only
SyntaxVerbose but powerfulSimpler

The Qt Auto-Tools (MOC, UIC, RCC)

Qt relies on three code-generation tools:

MOC — Meta-Object Compiler

Processes header files with Q_OBJECT macro and generates meta-object code for signals/slots and reflection.

set(CMAKE_AUTOMOC ON)  # handled automatically by CMake

UIC — UI Compiler

Converts .ui files (Qt Designer) into C++ header files (ui_mainwindow.h).

set(CMAKE_AUTOUIC ON)

RCC — Resource Compiler

Embeds binary resources (images, QML files) into the executable.

set(CMAKE_AUTORCC ON)

Build Configurations

# Configure (Debug)
cmake -B build -DCMAKE_BUILD_TYPE=Debug \
      -DCMAKE_PREFIX_PATH=/path/to/Qt/6.x/gcc_64

# Configure (Release)
cmake -B build -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_PREFIX_PATH=/path/to/Qt/6.x/gcc_64

# Build
cmake --build build --parallel $(nproc)

# Run
./build/MyApp

Qt Resource System (.qrc)

Embed files into your binary so they’re always available:

<!-- resources/resources.qrc -->
<RCC>
  <qresource prefix="/icons">
    <file>icons/app.png</file>
    <file>icons/logo.svg</file>
  </qresource>
  <qresource prefix="/qml">
    <file>qml/Main.qml</file>
  </qresource>
</RCC>

Access in C++:

QIcon icon(":/icons/app.png");
QFile file(":/qml/Main.qml");

Summary

ConceptPurpose
CMAKE_AUTOMOCAuto-run MOC for Q_OBJECT classes
CMAKE_AUTOUICAuto-compile .ui files
CMAKE_AUTORCCAuto-embed .qrc resources
qt_add_executableQt-aware executable target
find_package(Qt6)Locate installed Qt modules

Next tutorial → Signals & Slots