Skip to content

Alex0vSky/QtRoadTrafficSimulation

Repository files navigation

QtRoadTrafficSimulation - Road Traffic Simulator with Qt

C++ Qt License

Demo

A road traffic simulator with three different Qt-based visualization approaches. Cars and roads based on python solution from BilHim/trafficSimulator and yossidoctor/AI-Traffic-Lights-Controller

✨ Key Features

🏗️ Architecture

  • Flexible rendering system with three independent implementations
  • Separation of logic and presentation through Common, Simulation and Via components
  • Simulation state managed via State pattern

🚦 Simulation Model

  • Road network with straight sections and turns - AllRoads
  • Traffic light control with fixed cycles - TrafficSignal
  • Vehicle generation with different routes - VehicleGenerator
  • Realistic movement physics Vehicle including:
    • Acceleration/braking
    • Distance keeping
    • Traffic light response

🎨 Visualization

Three implementation approaches with common functionality:

  1. QGraphicsView:

    • Classic approach with main-thread rendering
    • Uses QGraphicsPolygonItem for drawing
    • Simpler implementation but less performant
  2. QQuickItem:

    • Modern Scene Graph rendering
    • Separate render thread (high performance)
    • Uses QSGGeometryNode for efficient drawing
  3. QQuickPaintedItem:

    • QPainter-based rendering in separate thread
    • More flexible but less performant than QQuickItem
    • Uses standard Qt painting methods

Visualization Architecture

Three implementations following a hierarchical class structure:

  1. BaseZoomableDraggableLoopLauncher/Main

Each layer adds specific functionality:

  • Zoomable: Mouse wheel zoom implementation
  • Draggable: Scene dragging/panning support
  • LoopLauncher: Main simulation loop handling

⚙️ Technical Highlights

  • Alternative QML resource locator - QrcLocatorReplacement:

    • Enables QML usage without Qt plugins in MSVC
    • Searches for QML files in multiple paths
    • Uses QQmlAbstractUrlInterceptor for qrc path redirection
  • MOC-free operation support:

    • Conditional compilation with A0S_QT_PRO
    • Alternative Q_OBJECT implementation via W_OBJECT/W_OBJECT_IMPL
    • Compilation without Qt's meta-object system
  • Unified polygon handling:

    • Consistent QPolygonF usage across all implementations
    • Qt6 compatibility adaptations:
      • Replaced deprecated DrawTriangleFan with DrawTriangleStrip
      • Replaced DrawLineLoop with DrawLineStrip
    • Benefits:
      • Compatibility with both Qt5 and Qt6
      • More modern and efficient rendering methods
      • Improved graphics performance

📦 Installation & Setup

Build Options

The project supports two build methods:

  1. Standard build - HelloQt.pro:

    • For Qt Creator and other IDEs
    • Uses standard Qt build system
  2. MOC-free build - HelloQt(Non_MocUic).sln:

    • For Visual Studio 2019
    • Specifically configured for Qt 5.15
    • Without Qt 5.15 you may encounter errors like:
      The code execution cannot proceed because Qt5Cored.dll was not found
      The code execution cannot proceed because Qt5Guid.dll was not found
      The code execution cannot proceed because Qt5Quickd.dll was not found
      The code execution cannot proceed because Qt5Widgetsd.dll was not found
      

Requirements

For Visual Studio 2019 Build

  • Qt 5.15 (exact version required)
  • Microsoft Visual Studio 2019 Community

For Qt Creator/Other IDEs

  • Qt 5.15 or later
  • Standard Qt build system
  • Any IDE supporting .pro files (Qt Creator, CLion, etc.)

🚀 Usage

  • Launch the application
  • Use mouse wheel to zoom
  • Drag the scene with left mouse button
  • Observe vehicle movement and traffic light operation

🤝 Contributing

PRs and questions are welcome! No strict requirements for contributors.

📚 Acknowledgments

📜 License

Project is MIT licensed. See LICENSE for details.

About

Road traffic simulation using Qt

Topics

Resources

License

Stars

Watchers

Forks