qml demo
This commit is contained in:
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_;
|
||||
|
||||
};
|
||||
28
src/main.cpp
28
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