

전의 QQmlContext를 이용한 방법과 코드만 조금 다를뿐이지 차이점을 잘..... 모르겠다..
클래스를 라이브러리화 해도 cpp에서 include 해주느냐 qml에서 import 해주느냐 차이지
아직의 나로서는..ㅠ
아.. 클래스 자체를 qml 객체화 시킨다는 것도 차이점이면 차이점인가?
아무튼 코드상의 차이점을 확인하기 쉽게 전에 사용했던 코드는 주석처리 하였다.
참고 할 차이점은 cpp에서 include가 제외되고 qml에서 import가 추가 되었다는 점이다.
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
//#include <QQmlContext>
#include "indicator.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
// Indicator indicator;
// engine.rootContext()->setContextProperty("Indicator", &indicator);
qmlRegisterType<Indicator>("Indicator", 1, 0, "Indicator");
engine.load(QUrl("qrc:/untitled/Main.qml"));
return app.exec();
}
Main.qml
import QtQuick
import QtQuick.Controls
import Indicator 1.0
Window {
width: 360
height: 570
visible: true
title: qsTr("Gunny Arduino Control")
// Connections {
// target: Indicator
// onLeftSignalSent: {
// Indicator.getLeftIndicatorVisible(leftIndicator.visible);
// }
// onRightSignalSent: {
// Indicator.getRightIndicatorVisible(rightIndicator.visible);
// }
// }
Indicator {
id: indicator
onLeftSignalSent: {
indicator.getLeftIndicatorVisible(leftIndicator.visible);
}
onRightSignalSent: {
indicator.getRightIndicatorVisible(rightIndicator.visible);
}
}
BusyIndicator {
id: leftIndicator
x: 40
y: 15
width: 100
height: 100
visible: true
}
BusyIndicator {
id: rightIndicator
x: 220
y: 15
width: 100
height: 100
visible: false;
}
Button {
id: leftButton
x: 5
y: 140
width: 170
height: 300
text: qsTr("좌측 깜빡이")
onClicked: {
// Indicator.sendLeftSignal();
indicator.sendLeftSignal();
}
}
Button {
id: rightButton
x: 185
y: 140
width: 170
height: 300
text: qsTr("우측 깜빡이")
onClicked: {
// Indicator.sendRightSignal();
indicator.sendRightSignal();
}
}
Button {
id: hazardButton
x: 5
y: 450
width: 350
height: 100
text: qsTr("비상등")
}
}
indicator.h
#pragma once
#include <QObject>
#include <QDebug>
class Indicator : public QObject
{
Q_OBJECT
public:
Indicator(QObject *parent = nullptr);
signals:
void leftSignalSent();
void rightSignalSent();
public slots:
void sendLeftSignal();
void sendRightSignal();
void getLeftIndicatorVisible(bool visible);
void getRightIndicatorVisible(bool visible);
private:
bool leftIndicatorVisible;
bool rightIndicatorVisible;
};
indicator.cpp
#include "indicator.h"
Indicator::Indicator(QObject *parent) : QObject(parent)
{
}
void Indicator::sendLeftSignal()
{
emit leftSignalSent();
}
void Indicator::sendRightSignal()
{
emit rightSignalSent();
}
void Indicator::getLeftIndicatorVisible(bool visible)
{
leftIndicatorVisible = visible;
qDebug() << "LeftIndicatorVisible Value : " << leftIndicatorVisible;
}
void Indicator::getRightIndicatorVisible(bool visible)
{
rightIndicatorVisible = visible;
qDebug() << "RightIndicatorVisible Value : " << rightIndicatorVisible;
}
'공부 > QT' 카테고리의 다른 글
[QT] QQmlContext(setContextProperty 함수)를 이용하여 qml 객체의 속성 값을 C++ 클래스 멤버변수에 대입하는 방법 (0) | 2023.08.13 |
---|---|
[QT] module "QtQuick.Controls" version 5.15 is not installed 해결법 (0) | 2023.08.12 |