날먹을 위한 몸부림/QT (PySide6)

python을 백앤드로 사용하는 qml 데모 - 1

프로그래밍하는 지팡이 2024. 6. 19. 21:24
import sys
from PySide6.QtCore import QObject, Slot
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
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()
backend = Backend()
engine.rootContext().setContextProperty("backend", backend)
engine.load("qml/main.qml")
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
view raw main.py hosted with ❤ by GitHub
import QtQuick
import QtQuick.Controls
ApplicationWindow {
visible: true
width: 640
height: 480
title: "Hello QML"
Button {
text: "Click me"
anchors.centerIn: parent
onClicked: backend.button_clicked()
}
}
view raw main.qml hosted with ❤ by GitHub