qml demo
This commit is contained in:
@@ -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 . $<TARGET_FILE:sonobulus>"
|
||||
COMMENT "windeployqt..."
|
||||
VERBATIM
|
||||
)
|
||||
endif()
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
33
src/Main.qml
Normal file
33
src/Main.qml
Normal file
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
24
src/TimerComponent.cpp
Normal file
24
src/TimerComponent.cpp
Normal file
@@ -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();
|
||||
|
||||
}
|
||||
33
src/TimerComponent.hpp
Normal file
33
src/TimerComponent.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QTimer>
|
||||
|
||||
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_;
|
||||
|
||||
};
|
||||
26
src/main.cpp
26
src/main.cpp
@@ -1,9 +1,31 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QQmlContext>
|
||||
|
||||
#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;
|
||||
// attach backend gui components
|
||||
qmlRegisterType<TimerComponent>("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();
|
||||
}
|
||||
Reference in New Issue
Block a user