From 6fba54f9c00efccaa3617b73a104a120707af805 Mon Sep 17 00:00:00 2001 From: Bliblank Date: Fri, 5 Jun 2026 22:26:39 -0500 Subject: [PATCH] qml demo --- CMakeLists.txt | 44 +++++++++++++++++++++++++++++++++++------- README.md | 3 ++- src/Main.qml | 33 +++++++++++++++++++++++++++++++ src/TimerComponent.cpp | 24 +++++++++++++++++++++++ src/TimerComponent.hpp | 33 +++++++++++++++++++++++++++++++ src/main.cpp | 28 ++++++++++++++++++++++++--- 6 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 src/Main.qml create mode 100644 src/TimerComponent.cpp create mode 100644 src/TimerComponent.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ec1c19..61857a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,14 +5,44 @@ project(sonobulus LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# add_library(sonobulus_core STATIC -# # empty -# ) +find_package(Qt6 REQUIRED COMPONENTS + Core + Quick +) -add_executable(sonobulus +qt_standard_project_setup() + +add_library(sonobulus_core STATIC + src/TimerComponent.cpp +) +target_link_libraries(sonobulus_core PRIVATE + Qt6::Core + Qt6::Quick +) + +qt_add_executable(sonobulus src/main.cpp ) -# target_link_libraries(sonobulus -# sonobulus_core -# ) +qt_add_qml_module(sonobulus + URI sonobulus + VERSION 1.0 + QML_FILES src/Main.qml +) + +target_link_libraries(sonobulus PRIVATE + sonobulus_core + Qt6::Core + Qt6::Quick +) + +install(TARGETS sonobulus) + +if (WIN32) + add_custom_command( + TARGET sonobulus POST_BUILD + COMMAND powershell -c "${Qt6_DIR}/../../../bin/windeployqt6.exe --qmldir . $" + COMMENT "windeployqt..." + VERBATIM + ) +endif() diff --git a/README.md b/README.md index 0112d98..a227a1c 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,8 @@ to produce somewhat well-sounding instruments and music performance. Prerequisites: CMake: https://cmake.org/download/ \ -QtWidgets: https://www.qt.io/development/download-qt-installer-oss +Qt6::QML: https://www.qt.io/development/download-qt-installer-oss \ +Note: you must export the Qt install location to your environment. E.g. add the following to a powershell profile: `$env:Qt6_DIR = "C:\Qt\6.10.1\msvc2022_64"` Compiler that supports C++20. Builds have been tested with GCC12 & MSVC17 diff --git a/src/Main.qml b/src/Main.qml new file mode 100644 index 0000000..c06abb3 --- /dev/null +++ b/src/Main.qml @@ -0,0 +1,33 @@ + +import QtQuick +import QtQuick.Controls + +import AppDemo + +ApplicationWindow { + visible: true + width: 600 + height: 400 + title: "sonobulus" + + TimerComponent { + id: timerComponent + } + + Column { + anchors.centerIn: parent + spacing: 20 + + Label { + text: timerComponent.seconds + " s" + font.pixelSize: 32 + horizontalAlignment: Text.AlignHCenter + } + + Button { + text: "Reset" + onClicked: timerComponent.reset() + } + } + +} diff --git a/src/TimerComponent.cpp b/src/TimerComponent.cpp new file mode 100644 index 0000000..c1aa3db --- /dev/null +++ b/src/TimerComponent.cpp @@ -0,0 +1,24 @@ + +#include "TimerComponent.hpp" + +TimerComponent::TimerComponent(QObject* parent) : QObject(parent) { + + // QTimer attach callback + connect(&timer_, &QTimer::timeout, this, &TimerComponent::increment); + timer_.start(1000); + +} + +void TimerComponent::reset() { + + seconds_ = 0; + emit secondsChanged(); + +} + +void TimerComponent::increment() { + + seconds_++; + emit secondsChanged(); + +} diff --git a/src/TimerComponent.hpp b/src/TimerComponent.hpp new file mode 100644 index 0000000..6ab906e --- /dev/null +++ b/src/TimerComponent.hpp @@ -0,0 +1,33 @@ + +#pragma once + +#include +#include + +class TimerComponent : public QObject { + + Q_OBJECT + Q_PROPERTY(int seconds READ seconds NOTIFY secondsChanged) + + public: + + explicit TimerComponent(QObject* parent = nullptr); + + int seconds() const { return seconds_; } + + public slots: + + void reset(); + + signals: + + void secondsChanged(); + + private: + + void increment(); + + int seconds_ = 0; + QTimer timer_; + +}; diff --git a/src/main.cpp b/src/main.cpp index 8863874..140d724 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,31 @@ #include +#include +#include +#include + +#include "TimerComponent.hpp" + int main(int argc, char* argv[]) { - std::cout << "hi mom !" << std::endl; + // create application + QGuiApplication app(argc, argv); + QQmlApplicationEngine engine; - return 0; -} \ No newline at end of file + // attach backend gui components + qmlRegisterType("AppDemo", 1, 0, "TimerComponent"); + + // load qml + //engine.loadFromModule("sonobulus", "Main"); + //engine.load(QUrl("qrc:/Main.qml")); + engine.load(QUrl::fromLocalFile("src/Main.qml")); // ugh + + if(engine.rootObjects().isEmpty()) { + std::cout << "engine is empty" << std::endl; + return -1; + } + + // execute app + return app.exec(); +}