이 예제는 signal slot을 활용하여 QML에서 화면 클릭 시 문구를 출력하는 예제입니다.
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "receiver.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
Receiver receiver;
QQmlContext* ctx = engine.rootContext();
ctx->setContextProperty("receiver", &receiver);
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
receiver.sendToQml(43);
return app.exec();
}
코드 : https://pastebin.com/jFVY9HVb
#ifndef RECEIVER_H
#define RECEIVER_H
#include <QObject>
class Receiver : public QObject
{
Q_OBJECT
public:
explicit Receiver(QObject *parent = 0);
signals:
void sendToQml(int count);
public slots:
void receiveFromQml(int count);
};
#endif // RECEIVER_H
#include "receiver.h"
#include <QDebug>
Receiver::Receiver(QObject *parent) :
QObject(parent)
{
}
void Receiver::receiveFromQml(int count) {
qDebug() << "Received in C++ from QML:" << count;
}
import QtQuick 2.2
import QtQuick.Window 2.1
Window {
id: test
visible: true
width: 400
height: 500
Connections {
target: receiver
onSendToQml: {
console.log("Received in QML from C++: " + count)
}
}
MouseArea {
anchors.fill: parent
onClicked: { // 마우스 터치 시 QML 에서 C++로 통신
receiver.receiveFromQml(42);
}
}
Text {
text: qsTr("Press me to send a signal to C++")
anchors.centerIn: parent
}
}
TEMPLATE = app
QT += qml quick
SOURCES += main.cpp \
receiver.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
receiver.h
[C++ QT QML] 리눅스에서 QML WIFI list 불러오게 구현 (0) | 2021.06.16 |
---|---|
[C++ QT QML] Signal slot으로 C++에서 QML로 데이터 전송 (0) | 2021.06.15 |
[C++ QT widget] Linux Ubuntu에서 Makefile, qmake, .pro 설명 및 화면 구현하기 (0) | 2021.06.14 |
[C++ QT QML] Linux Ubuntu에서 Makefile 만들기 버전[2](Windows/Ubuntu) (0) | 2021.06.14 |
[C++ QT QML] Visual Studio Code에서 Makefile 만들기 버전[1](Windows/Ubuntu) (0) | 2021.06.14 |