This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PySide6.QtCore import QObject, Slot | |
from PySide6.QtGui import QGuiApplication | |
from PySide6.QtQml import QQmlApplicationEngine, QmlElement | |
QML_IMPORT_NAME = "hamster.backend" | |
QML_IMPORT_MAJOR_VERSION = 1 | |
@QmlElement | |
class Backend(QObject): | |
def __init__(self): | |
super().__init__() | |
self.counter = 0 | |
@Slot() | |
def button_clicked(self): | |
self.counter += 1 | |
print(f"Button clicked {self.counter} times") | |
if __name__ == "__main__": | |
app = QGuiApplication(sys.argv) | |
engine = QQmlApplicationEngine() | |
engine.load("qml/main.qml") | |
if not engine.rootObjects(): | |
sys.exit(-1) | |
sys.exit(app.exec()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick | |
import QtQuick.Controls | |
import hamster.backend 1.0 | |
ApplicationWindow { | |
visible: true | |
width: 640 | |
height: 480 | |
title: "Hello QML" | |
Backend { | |
id: backend | |
} | |
Button { | |
text: "Click me" | |
highlighted: true | |
anchors.centerIn: parent | |
onClicked: backend.button_clicked() | |
} | |
} |
https://boa9448.tistory.com/41
python을 백앤드로 사용하는 qml 데모 - 1
HTML 삽입미리보기할 수 없는 소스
boa9448.tistory.com
위와 다른 방식으로 qml에 파이썬 객체를 노출함
QML_IMPORT_NAME = "hamster.backend"
QML_IMPORT_MAJOR_VERSION = 1
위와 같이 노출할때 사용할 패키지 이름과 버전을 지정하고
@QmlElement
class Backend(QObject):
노출할 클래스를 QmlElement로 장식해줌
import hamster.backend 1.0
그리고 QML에서 포함
Backend {
id: backend
}
Button {
text: "Click me"
highlighted: true
anchors.centerIn: parent
onClicked: backend.button_clicked()
}
그리고 노출된 객체를 사용
결과는 이전과 같음

'날먹을 위한 몸부림 > QT (PySide6)' 카테고리의 다른 글
qml에 material 스타일 적용하기 (2) | 2024.06.21 |
---|---|
python을 백앤드로 사용하는 qml 데모 - 1 (2) | 2024.06.19 |
pyside6에서 윈도우 핫키 컨트롤 사용하기(공용 컨트롤) (2) | 2022.08.30 |
python SetWindowDisplayAffinity (2) | 2022.08.22 |
c++ qt에 material 테마 적용하기 (0) | 2022.05.26 |