달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

<<Previous - Display Order(2)


QML Object를 동적으로 생성하는 방법에 대한 설명은 아래 링크를 따라 가면 된다.

Dynamic QML Object Creator( http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html )


QML 파일을 동적으로 생성하기 위해서는 Qt 객체의 함수 두 개를 사용해야 한다.

첫 번째로 Qt.createComponent() 를 이용하여 Component를 생성하거나 Qt.createQmlObject() 를 사용하여

문자열로 QML 객체를 정의하여 만들 수 있다.

두 번째로 QML 객체가 생성되면 Qt.createObject() 를 이용하여 생성된 객체에 속성 값을 설정한다.


첫 변째 두 함수의 차이는 Qt.createComponent()는 첫 번째 인자로 QML 객체(소스) URL을 전달하고 Qt.createQmlObject()은 첫 번째 인자로 QML 객체를 정의하고 있는 string을 넘겨준다.


Qt.createComponent()를 이용하여 간단한 javascript함수를 만들면 아래와 같다.

function create_qml_object(qmlfile_url, parent_id) {

    if(qmlfile_url == "") return undefined;


    var component = Qt.createComponent(qmlfile_url);

    if(component.status == Component.Ready) {

        var appobj = component.createObject(parent_id, {"x":0, "y":0, "width":0, "height":0});

        if (appobj != null) {

            appobj.visible = false;

            component.destroy();

            return appobj;

        }

    }

    component.destroy();

    return undefined;

}



create_qml_object 함수를 이용한 예제:

[main.qml]


[Rect.qml]


QML Display Order를 할 수 있는 z Order, Loader 객체 이용, 객체 Creator 세가지 방법에 대해 모두 알아봤다.

사실 Loader객체와 객체를 Creator하는 방법은 QML Display Order에 상관있는 부분은 아니다. 하지만 QML 파일을  필요할 때 생성하고 삭제 하면 Display하는데 조금이나마 유리할 수 있다. 

exQMLCreator.tar.gz


Posted by 생짜
|