Created
April 21, 2020 13:56
-
-
Save aerialist/bc8d5c13951715a965d5b997f27c3b70 to your computer and use it in GitHub Desktop.
PySide2 QML boiler plate
This file contains 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
# This Python file uses the following encoding: utf-8 | |
# https://qmlbook.github.io/ch18-python/python.html | |
# This bug has already been fixed | |
# We can connect in QML | |
# https://bugreports.qt.io/browse/PYSIDE-634 | |
# https://wiki.qt.io/Qt_for_Python_Signals_and_Slots | |
import sys | |
import random | |
# from PySide2.QtWidgets import QApplication | |
# QGuiApplication - base class + GUI capabilities. Use it in QML applications. | |
# QApplication - base class + GUI + support for widgets. | |
# Use it in QtWidgets applications. | |
from PySide2.QtGui import QGuiApplication | |
from PySide2.QtQml import QQmlApplicationEngine | |
from PySide2.QtCore import QUrl, QObject, Signal, Slot | |
class NumberGenerator(QObject): | |
nextNumber = Signal(int, name="nextNumber", arguments=['number']) | |
def __init__(self): | |
QObject.__init__(self) | |
@Slot() | |
def giveNumber(self): | |
self.nextNumber.emit(random.randint(0, 99)) | |
if __name__ == '__main__': | |
app = QGuiApplication(sys.argv) | |
# app = QApplication([]) | |
engine = QQmlApplicationEngine() | |
number_generator = NumberGenerator() | |
engine.rootContext().setContextProperty("numberGenerator", number_generator) | |
engine.load(QUrl("main.qml")) | |
if not engine.rootObjects(): | |
sys.exit(-1) | |
sys.exit(app.exec_()) |
This file contains 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
{ | |
"files": ["main.py","main.qml"] | |
} |
This file contains 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 2.0 | |
import QtQuick.Window 2.0 | |
import QtQuick.Controls 2.0 | |
Window { | |
id: root | |
width: 640 | |
height: 480 | |
visible: true | |
title: "Hello Python World!" | |
Flow { | |
Button { | |
text: "Give me a number!" | |
onClicked: numberGenerator.giveNumber(); | |
} | |
Label { | |
id: numberLabel | |
text: "no number" | |
} | |
Connections { | |
target: numberGenerator | |
onNextNumber: { | |
numberLabel.text = number | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment