조회 수 6814 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

이 게시물에서는 Qt 6.0에 추가된 C++ 바인딩 기능을 잘 사용하는 방법을 설명하고 간단한 예제를 제공한다.

다음의 간단한 클래스와 같이 QBindable<int>를 반환하는 새로운 bindableX() 메서드를 추가하고 Q_PROPERTY 매크로에서 메타 개체 시스템에 이에 대해 알렸다. Qt 6.0부터 BINDABLE 을 사용하여 바인딩 가능한 속성을 지정할 수 있다.

myobject.h

#include <QObject>

class MyObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int x READ x WRITE setX BINDABLE bindableX)

public:
    explicit MyObject(QObject *parent = nullptr);
    int x() { return xData; }
    void setX(int x) { xData = x; }
    QBindable<int> bindableX() { return &xData; }

signals:
    void xChanged();

private:
    // Qt5에서는 아래 줄이 "int xData;" 였다.
    Q_OBJECT_BINDABLE_PROPERTY(MyObject, int, xData, &MyObject::xChanged)

};

Qt 6 이전에 setter의 구현은 새 값과 기존 값을 비교하여 다를 경우에만 클래스 멤버변수를 업데이트하고 시그널을 방출했다. Qt 6.0 이상에서는 이 동일한 기능이 한 줄만 포함하도록 단순화되었다. 현재 값과 새 값에 대한 비교가 API 코드에서 수행되기 때문이다.

바인딩을 설정하거나 알림을 등록할 수 있다. 예를 들어 다음의 예제처럼 setBinding() 호출하여 MyObject의 x 속성에 대한 바인딩을 설정한다. 또 onValueChanged() 를 통해 속성 값이 변경될 때마다 호출되는 콜백을 등록할 수 있다. 여기서는 myobject1의 x 값이 변경되면 myobject2에 설정된 바인딩을 제거하도록했다.

main.cpp

...
#include "myobject.h"

int main(int argc, char *argv[])
{
...
    MyObject myobject1;
    MyObject myobject2;

    // 'x'프로퍼티 값 설정.
    myobject1.setX(50);

    // myobject1 의 'x'프로퍼티를 myobject2 의 'x'프로퍼티에 바인딩.
    myobject2.bindableX().setBinding([&](){
        return myobject1.x() + 50;
    });

    auto h1 = myobject1.bindableX().onValueChanged([&](){
        qDebug() << "myobject1 'x' Changed.";
        qDebug() << myobject2.bindableX().hasBinding();

        // 현재 설정된 바인딩을 제거.
        myobject2.bindableX().takeBinding();

        qDebug() << myobject2.bindableX().hasBinding();
    });

    QQmlApplicationEngine engine;
...
    // QML에 노출시킴.
    engine.rootContext()->setContextProperty("myobject1", &myobject1);
    engine.rootContext()->setContextProperty("myobject2", &myobject2);
...
}

Rectangle 의 width 는 myobject2 의 x 에 바인딩 되었으며 height 은 width 와 같다. Rectangle 영역을 마우스로 클릭하면 myobject1 의 x 값을 10 증가 시키도록했다.

main.qml

...
    Rectangle {
        width: myobject2.x // myobject2 의 'x'프로퍼티 바인딩.
        height: width

        border.width: 1

        anchors.centerIn: parent

        onWidthChanged: console.log("width:", width)
        onHeightChanged: console.log("height:", height)

        MouseArea {
            anchors.fill: parent
            onClicked: {
                myobject1.x += 10 // myobject1의 'x'값을 10 증가시킴.
            }
        }
    }
...

사각형을 클릭하면 시각적으로 어떻게 변하는지 알 수 있으며 콘솔 출력은 다음과 같다

qml: width: 100
qml: height: 100
qml: width: 110
qml: height: 110 
true 
myobject1 'x' Changed. 
false
TAG •

  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views131099
    read more
  2. No Image

    clazy 로 13개의 시그널, 슬롯 오류 해결

    Date2022.08.23 CategoryGeneral and Desktop Bymakersweb Views8527
    Read More
  3. No Image

    Qt 스마트 포인터 (QSharedPointer, QScopedPointer, QPointer)

    Date2022.08.18 CategoryC++ Class Bymakersweb Views5993
    Read More
  4. Qt 6.4에 추가될 Qt Quick 3D Physics

    Date2022.08.07 CategoryQt 6 Bymakersweb Views4519
    Read More
  5. No Image

    HTTPS URL을 연결할 때 SslHandshakeFailedError 오류

    Date2022.07.31 CategoryC++ Class Bymakersweb Views5379
    Read More
  6. No Image

    단일 인스턴스 Qt 응용 프로그램(Single-instance Application)

    Date2022.06.23 CategoryGeneral and Desktop Bymakersweb Views5972
    Read More
  7. Qt로 작성된 iOS 앱에서 시리얼 통신

    Date2022.04.30 CategoryMobile and Embedded Bymakersweb Views9559
    Read More
  8. No Image

    VirtualKeyboard 스타일 커스터 마이징

    Date2022.03.13 CategoryGeneral and Desktop Bymakersweb Views5770
    Read More
  9. No Image

    성능 고려 및 제안 사항

    Date2022.03.07 Bymakersweb Views4425
    Read More
  10. No Image

    Binding 타입으로 객체 속성 간 묶기

    Date2022.03.04 CategoryQML and Qt Quick Bymakersweb Views6719
    Read More
  11. No Image

    Qt Bluetooth Low Energy 개요

    Date2022.02.13 CategoryMobile and Embedded Bymakersweb Views5697
    Read More
  12. Qt Android 앱에 AdMob 배너달기

    Date2021.12.04 CategoryMobile and Embedded Bymakersweb Views3778
    Read More
  13. No Image

    Qt 6의 C++ 프로퍼티 바인딩 예제

    Date2021.11.01 CategoryQt 6 Bymakersweb Views6814
    Read More
  14. QML에서 앵커(anchors)로 위치 지정

    Date2021.10.05 CategoryQML and Qt Quick Bymakersweb Views10019
    Read More
  15. No Image

    안드로이드용 Qt 6.2

    Date2021.10.02 CategoryQt 6 Bymakersweb Views10111
    Read More
  16. Qt 응용프로그램에서 PDF 문서 렌더링

    Date2021.09.23 CategoryGeneral and Desktop Bymakersweb Views5627
    Read More
  17. QML에서 Websocket 서버와 통신

    Date2021.09.18 CategoryQML and Qt Quick Bymakersweb Views5381
    Read More
  18. No Image

    QML 코딩 규칙

    Date2021.09.05 CategoryQML and Qt Quick Bymakersweb Views9139
    Read More
  19. QML 에서 QR코드 생성

    Date2021.08.20 CategoryQML and Qt Quick Bymakersweb Views5330
    Read More
  20. No Image

    앱을 종료할 때 QML 바인딩 오류를 피하는 방법

    Date2021.08.08 CategoryQML and Qt Quick Bymakersweb Views9788
    Read More
  21. Qt 응용프로그램에서 Lottie Animation사용

    Date2021.05.30 CategoryQML and Qt Quick Bymakersweb Views5561
    Read More
Board Pagination Prev 1 2 3 4 5 9 Next
/ 9