한국어
Qt
 

엑세스할 수 없는 속성에 바인딩

C++에서 QML로 노출하거나 내보낸 객체의 속성과 같이 QML에서 직접 인스턴스화하지 않은 다른 개체의 속성에 바인딩해야 하는 경우가 있다. Binding 타입을 사용하여 객체들의 속성간 동적 바인딩을 생성할 수 있다. 예를 들어 myobject1.x 속성을 Binding을 사용하여 업데이트할 수 있다. 즉, 텍스트가 변경되면 C++ 속성 x가 자동으로 업데이트된다.

C++

...
class MyObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString x READ x WRITE setX)
...
}

...
MyObject myobject1;
engine.rootContext()->setContextProperty("myobject1", &myobject1);
...

 

QML

...
    TextEdit { 
        id: myTextField 
        text: "Please type here..." 
    }

    Binding { 
        target: myobject1
        property: "x" 
        value: myTextField.text 
    }
...

 

 

조건부 바인딩

특정 조건이 충족됐을 때만 속성 값을 수정하도록 설정할 수 있다.

관련 있는 모든 분기에 대해 값을 제공해야 하므로 직접 바인딩으로는 이 작업을 수행할 수 없는 경우가 많다.

예를 들어 아래 예제 코드는 마우스를 놓을 때마다 경고를 표시한다. 마우스를 누르지 않았을 때 바인딩 값이 정의되지 않았기 때문이다.

...
    // produces warning: "Unable to assign [undefined] to double value"
    property int value: if (mouse.pressed) mouse.mouseX

    Rectangle {
...
        MouseArea {
            id: mouse
            anchors.fill: parent
        }
    }
...

 

다음과 같은 코드를 사용하면 당장 경고를 회피할 수 는 있다. 이것은 바인딩이 끊어진 채로 유지되므로 바람직하지 않다.

...
    property int value
...
        MouseArea {
            id: mouse
            anchors.fill: parent
            onPressed: {
                value = mouseX
            }
        }
...

 

Binding 을 사용하는 것이 바람직하다. restoreMode 속성은 바인딩이 비활성화 되었을 때 원래 값을 복원해야 하는지 여부와 복원 방법을 설명하는 데 사용할 수 있다. 기본 값(default value)은 Binding.RestoreBinding 로 설정된다.

...
    property int value

    Rectangle {
...
        MouseArea {
            id: mouse
            anchors.fill: parent
        }
    }

    Binding on value {
        when: mouse.pressed
        value: mouse.mouseX
        restoreMode: Binding.RestoreNone
    }
...

 

https://doc.qt.io/qt-5/qml-qtqml-binding.html

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 86419
179 가상키보드(Qt Virtual Keyboard)를 사용하는 방법 [32] file makersweb 2019.05.03 222215
178 콘솔에서 사용자 입력받기 file makersweb 2020.03.22 51868
177 QString 문자열 다루기 예제 운영자 2019.01.26 40548
176 Windows에서 Qt 설치 따라하기 file makersweb 2019.10.14 30971
175 Qt의 시그널 슬롯 시스템 file makersweb 2015.10.20 23751
174 QThread 소개 및 예제 makersweb 2019.12.25 19672
173 QtCreator Design으로 GUI만들기 (QML로 만드는 Hello World -2) [1] file makersweb 2019.05.26 15038
172 초보자를 위한 첫번째 프로젝트 - QML로 만드는 Hello World file makersweb 2018.03.16 14560
171 Qt 프로그래밍의 시작 makersweb 2015.10.25 14431
170 Qml과 C++로 구현하는 GUI어플리케이션 file makersweb 2018.12.25 14067
169 QML과 QtQuick 모듈 개념과 기본 타입들 makersweb 2019.04.26 13460
168 Windows에서 라즈베리파이3용 Qt5.10.0 크로스컴파일 [20] file makersweb 2018.02.23 13104
167 Qt의 오픈소스 라이센스 소개 file makersweb 2019.12.15 12677
166 Qml 기본 컴포넌트 강좌 (1) file makersweb 2019.01.03 12156
165 QtSerialPort를 사용한 시리얼(Serial)통신 [3] makersweb 2019.05.21 12036
164 Qt Installer Framework - 패키징, 설치프로그램 제작 file makersweb 2018.10.14 11733
163 Qt 응용프로그램 배포(windows) file makersweb 2018.10.10 11431
162 Ubuntu Linux에서 Qt Creator 설치 file makersweb 2016.03.06 10687
161 Qt의 스레드간 시그널 슬롯의 커넥션타입 [1] makersweb 2015.10.24 10241
160 Qt애플리케이션 객체(QCoreApplication, QGuiApplication, QApplication) 에 대해서 makersweb 2019.11.11 10196