한국어
파이썬
 

PyQt4 Python + QML with PyQt4

makersweb 2015.03.24 17:17 조회 수 : 7850

PyQt4모듈을 이용해서 QML을 사용하여 GUI어플리케이션을 개발할 수 있다.

 

예를 들면 다음과 같다.

from PyQt4.QtDeclarative import QDeclarativeView
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl

app = QApplication([])

view = QDeclarativeView()
view.setSource(QUrl("/path/to/demo.qml"))
view.show()

app.exec_()


QML로 데이터를 넘겨주기 위한 방법은 두가지가 있는데 하나는 변수가 이미 QML에 정의되어 있는 경우이고,

다른 하나는 변수 프로퍼티를 설정하는 것이다.

 

미리정의된 변수

test.qml

import Qt 4.7

Rectangle {
    id: test
    width: 100; height: 30

    Text {
        anchors.fill:parent;
        text: textData;
    }
}


test.py

from PyQt4.QtDeclarative import QDeclarativeView
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl, QString


app = QApplication([])

view = QDeclarativeView()
rootContext = view.rootContext()
rootContext.setContextProperty("textData", QString("hi"))
view.setSource(QUrl("test.qml"))
view.show()
app.exec_()

 

변수설정.

test.qml

import Qt 4.7

Rectangle {
    id: test
    property string textData;
    width: 100; height: 30

    Text {
        anchors.fill:parent;
        text: textData;
    }
}


test.py

from PyQt4.QtDeclarative import QDeclarativeView
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl, QString


app = QApplication([])

view = QDeclarativeView()
view.setSource(QUrl("test.qml"))
rootObject = view.rootObject()
rootObject.setProperty("textData", QString("hi"))
view.show()
app.exec_()

 

 

번호 제목 글쓴이 날짜 조회 수
24 pybind11 에 대해서 makersweb 2023.07.23 1771
23 Qt For Python(PySide2) QML 프로젝트 예제 file makersweb 2019.10.17 4751
22 명령어 한줄로 내컴퓨터를 웹서버로 file 운영자 2019.01.25 3421
21 pydbus 바인딩을 이용하여 서비스 데몬과 D-Bus통신 file makersweb 2018.03.12 2045
20 Boost Python을 이용하여 python을 위한 C++ API 바인딩 [5] file makersweb 2017.01.08 9019
19 [Python]EXE또는 DLL파일의 버전정보를 알아내기위한 몇가지 방법 makersweb 2015.06.25 8631
18 QML 및 Window 투명처리 file makersweb 2015.04.22 3983
17 [pyqt4]QTimer 예제 - 버튼을 누르면 3초후 함수 또는 메소드 호출 makersweb 2015.04.01 9596
» Python + QML with PyQt4 makersweb 2015.03.24 7850
15 print를 로그파일로 생성하기 (log출력 Redirection) makersweb 2015.03.18 5471
14 외부 프로그램 실행 pjk 2015.02.10 15253
13 우분투에 Python새 버전 설치 사용법 pjk 2015.02.10 3137
12 How to Use Freeze pjk 2014.09.06 4707
11 [PyQt4]개발 프로그램 버전표시 메세지 박스 pjk 2014.09.02 7640
10 [PyQt4]여러가지 버튼 예제 file pjk 2014.08.29 13678
9 Python 문자열 관련 함수 레퍼런스 pjk 2014.08.29 6574
8 [PyQt4]스레드 및 ProgressBar 예제 코드 file pjk 2014.08.26 8236
7 [PyQt4]multiprocess 예제 코드 pjk 2014.08.26 8136
6 [PyQt4]마우스 클릭 이벤트 예제 코드 pjk 2014.08.26 6770
5 [PyQt4]폴더 또는 파일을 드레그하여 그 경로를 LineEdit로 가져오기 file pjk 2014.08.22 13508