# qt-learning/multithreading-with-qt architecture

> Architecture diagram of the GitHub repository qt-learning/multithreading-with-qt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/qt-learning/multithreading-with-qt
- Repository: https://github.com/qt-learning/multithreading-with-qt

## Overview

This project is a hands‐on course repository titled “Multithreading-With-Qt” designed for the “Let's Get Thready” course. Its primary purpose is to demonstrate various methods of implementing multithreading (using QThreads, Workers, and QRunnable) within a Qt application framework. The application itself is a graphical sample (a basic Raycaster showing a red sphere) that is re-implemented in different ways across multiple subdirectories to illustrate the effects and patterns of multithreading.

##### Main Components
a. Base Application (Raycaster App):
- Core functionality provided in the “00-Start” directory. It includes the basic GUI (mainwindow.cpp, mainwindow.h, mainwindow.ui) and the primary computational component (raycaster.cpp/h).
b. Multithreading Variants:
- “01-QThreads”: Demonstrates using QThreads derived or managed directly.
- “02-Workers”: Implements a worker pattern for thread offloading.
- “03-QRunnable”: Uses the QRunnable interface typically in combination with Qt’s thread pool.
c. Build System:
- Each folder contains its own CMakeLists.txt indicating the use of CMake as the build system and project configuration tool.
d. Supporting Infrastructure:
- Common elements like the Qt Creator integration, and additional resources as provided in the README.

##### Relationships and Interactions
- The UI Layer (MainWindow, defined by mainwindow.cpp/h/ui in each subfolder) drives application interactions and renders results.
- The computational components (raycaster or imagerenderer) are integrated in each variant and handle the core graphical logic.
- In “01-QThreads”: the main UI spawns custom QThread instances.
- In “02-Workers”: the UI or main thread dispatches tasks to worker objects scheduled on separate threads.
- In “03-QRunnable”: the application enqueues tasks as QRunnables in a thread pool.
- The build configuration (CMake) supports each subdirectory as an independent module demonstrating specific approaches.

##### Architectural Patterns and Key Principles
- The layered architecture: separation between user interface, business logic (raycaster/imagerenderer), and multithreading techniques.
- Each variation exemplifies how concurrency is managed differently in Qt:
- QThread subclassing is shown in “01-QThreads”.
- Worker objects abstract threading behind the scenes in “02-Workers”.
- Task-based concurrency using QRunnable is documented in “03-QRunnable”.
- Reusability and modularity: while the UI and raycaster code are common in purpose, the threading mechanism is abstracted into different modules.

##### Relevant Technologies
- Qt Framework: for GUI elements (widgets, signals/slots) and multithreading support.
- CMake: for project configuration and build process.
- Specific multithreading classes: QThread, Worker patterns, and QRunnable, which are central to the project’s learning objectives.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Multithreading-With-Qt"]:::project

    subgraph "00-Start: Base Application"
        Start_UI("MainWindow UI"):::ui
        Start_Raycaster("Raycaster Logic"):::logic
        Start_UI -->|"triggers"| Start_Raycaster
        Start_Raycaster -->|"updates"| Start_UI
    end

    subgraph "01-QThreads: QThreads Variant"
        QThreads_UI("MainWindow UI"):::ui
        QThreads_Thread("QThread Manager"):::thread
        QThreads_Raycaster("Raycaster Logic"):::logic
        QThreads_UI -->|"triggers"| QThreads_Thread
        QThreads_Thread -->|"calls"| QThreads_Raycaster
        QThreads_Raycaster -->|"updates"| QThreads_UI
    end

    subgraph "02-Workers: Workers Variant"
        Workers_UI("MainWindow UI"):::ui
        Workers_Worker("Worker Manager"):::thread
        Workers_Raycaster("Raycaster Logic"):::logic
        Workers_UI -->|"triggers"| Workers_Worker
        Workers_Worker -->|"calls"| Workers_Raycaster
        Workers_Raycaster -->|"updates"| Workers_UI
    end

    subgraph "03-QRunnable: QRunnable Variant"
        QRunnable_UI("MainWindow UI"):::ui
        QRunnable_Thread("QRunnable Scheduler"):::thread
        QRunnable_Raycaster("Raycaster Logic"):::logic
        QRunnable_ImageRenderer("ImageRenderer"):::logic
        QRunnable_UI -->|"triggers"| QRunnable_Thread
        QRunnable_Thread -->|"calls_Raycaster"| QRunnable_Raycaster
        QRunnable_Thread -->|"calls_ImageRenderer"| QRunnable_ImageRenderer
        QRunnable_Raycaster -->|"updates"| QRunnable_UI
        QRunnable_ImageRenderer -->|"updates"| QRunnable_UI
    end

    subgraph "Build & Deployment"
        Build("CMake Build System"):::build
        Resources("Project Resources"):::res
    end

    A --> Start_UI
    A --> QThreads_UI
    A --> Workers_UI
    A --> QRunnable_UI
    A --> Build
    A --> Resources

    %% Click Events for Base Application
    click Start_UI "https://github.com/qt-learning/multithreading-with-qt/blob/main/00-Start/mainwindow.cpp"
    click Start_Raycaster "https://github.com/qt-learning/multithreading-with-qt/blob/main/00-Start/raycaster.cpp"

    %% Click Events for QThreads Variant
    click QThreads_UI "https://github.com/qt-learning/multithreading-with-qt/blob/main/01-QThreads/mainwindow.cpp"
    click QThreads_Raycaster "https://github.com/qt-learning/multithreading-with-qt/blob/main/01-QThreads/raycaster.cpp"

    %% Click Events for Workers Variant
    click Workers_UI "https://github.com/qt-learning/multithreading-with-qt/blob/main/02-Workers/mainwindow.cpp"
    click Workers_Raycaster "https://github.com/qt-learning/multithreading-with-qt/blob/main/02-Workers/raycaster.cpp"

    %% Click Events for QRunnable Variant
    click QRunnable_UI "https://github.com/qt-learning/multithreading-with-qt/blob/main/03-QRunnable/mainwindow.cpp"
    click QRunnable_Raycaster "https://github.com/qt-learning/multithreading-with-qt/blob/main/03-QRunnable/raycaster.cpp"
    click QRunnable_ImageRenderer "https://github.com/qt-learning/multithreading-with-qt/blob/main/03-QRunnable/imagerenderer.cpp"

    %% Click Event for Build System
    click Build "https://github.com/qt-learning/multithreading-with-qt/blob/main/00-Start/CMakeLists.txt"

    %% Click Event for Project Resources
    click Resources "https://github.com/qt-learning/multithreading-with-qt/blob/main/README.md"

    %% Styles
    classDef ui fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef logic fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef thread fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef build fill:#D5F5E3,stroke:#1E8449,stroke-width:2px;
    classDef res fill:#E8DAEF,stroke:#6C3483,stroke-width:2px;
    classDef project fill:#D6EAF8,stroke:#2874A6,stroke-width:2px;
```
