Then your main.py would look like this
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
from PyQt5.QtWidgets import QApplication
import sys
class MainWindow(QQuickView):
def __init__(self):
super().__init__()
self.setSource(QUrl('sample.qml'))
self.rootContext().setContextProperty("MainWindow", self)
self.show()
@pyqtSlot('QString')
def Print(self, value):
print(value)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())
And sample.qml like so
import QtQuick 2.0
import QtQuick.Controls 2.2
Rectangle {
width: 200; height: 200
Button {
text: "print Hello World"
onClicked: MainWindow.Print('hello world')
}
}
You can find more information in the docs
http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html
|