Last active
November 20, 2021 07:30
-
-
Save OlegJakushkin/739869af621d6795e440ec4adb05b410 to your computer and use it in GitHub Desktop.
Qt Quick 101
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "handler.h" | |
#include<QDebug> | |
Handler::Handler(QObject *parent) : QObject(parent) | |
{ | |
timesCalled = 0; | |
} | |
void Handler::buttonClicked() | |
{ | |
qInfo("callsed from C++ handler!"); | |
timesCalled++; | |
valueChanged(timesCalled); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HANDLER_H | |
#define HANDLER_H | |
#include <QObject> | |
class Handler : public QObject | |
{ | |
Q_OBJECT | |
int timesCalled; | |
public: | |
explicit Handler(QObject *parent = nullptr); | |
public slots: | |
void buttonClicked(); | |
signals: | |
void valueChanged(int newValue); | |
}; | |
#endif // HANDLER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QGuiApplication> | |
#include <QQmlApplicationEngine> | |
#include <QtQuickControls2/QtQuickControls2> | |
#include <QQmlContext> | |
#include "handler.h" | |
int main(int argc, char *argv[]) | |
{ | |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) | |
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); | |
#endif | |
QGuiApplication app(argc, argv); | |
QQmlApplicationEngine engine; | |
const QUrl url(QStringLiteral("qrc:/main.qml")); | |
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, | |
&app, [url](QObject *obj, const QUrl &objUrl) { | |
if (!obj && url == objUrl) | |
QCoreApplication::exit(-1); | |
}, Qt::QueuedConnection); | |
auto ctx = engine.rootContext(); | |
auto handler = Handler(); | |
ctx->setContextProperty("handler", &handler); | |
engine.load(url); | |
auto object = engine.rootObjects()[0]; | |
auto myButton = object->findChild<QObject*>("myButton3"); | |
QObject::connect(myButton, SIGNAL(clicked()), &handler, SLOT(buttonClicked())); | |
return app.exec(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.15 | |
import QtQuick.Window 2.15 | |
import './' as MyComponents | |
Window { | |
width: 640 | |
height: 480 | |
visible: true | |
title: qsTr("my test app") | |
MyComponents.MyApp { | |
id: myComponent | |
anchors.fill: parent | |
} | |
Connections { | |
target: handler | |
onValueChanged: { | |
console.log("Data: " + newValue) | |
var str_in = "Data: " + newValue | |
myComponent.pulicText.text = "Changed to: " + str_in | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.0 | |
import QtQuick.Controls 2.15 | |
GroupBox { | |
property Text pulicText: textToChange | |
id: groupBox | |
x: 0 | |
y: 0 | |
width: 215 | |
height: 137 | |
Button { | |
x: 104 | |
y: 86 | |
text: 'My button 1!' | |
anchors.right: parent.right | |
anchors.bottom: parent.bottom | |
anchors.rightMargin: 5 | |
anchors.bottomMargin: 5 | |
onClicked: groupBox.f() | |
} | |
Button { | |
text: 'My button 2!' | |
anchors.left: parent.left | |
anchors.top: parent.top | |
anchors.leftMargin: 5 | |
anchors.topMargin: 5 | |
onClicked: {console.log("hello from 2!")} | |
} | |
Button { | |
text: "My button 3!" | |
objectName: "myButton3" | |
id: myButton3 | |
anchors.left: parent.horizontalCenter | |
anchors.right: parent.horizontalCenter | |
anchors.top: parent.verticalCenter | |
anchors.bottom: parent.verticalCenter | |
anchors.rightMargin: -53 | |
anchors.bottomMargin: -17 | |
anchors.leftMargin: -54 | |
anchors.topMargin: -18 | |
} | |
Text { | |
objectName: "textToChange" | |
id: textToChange | |
text: "beforeChange" | |
anchors.left: parent.horizontalCenter | |
anchors.right: parent.horizontalCenter | |
anchors.top: parent.verticalCenter | |
anchors.bottom: parent.verticalCenter | |
anchors.rightMargin: -153 | |
anchors.leftMargin: -52 | |
anchors.bottomMargin: -48 | |
anchors.topMargin: 14 | |
} | |
function f() {console.log("hello! from groupBox")} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment