Skip to content

Instantly share code, notes, and snippets.

@csaez
Last active October 5, 2017 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csaez/2f160264163e07cbd4e11a8718281f4a to your computer and use it in GitHub Desktop.
Save csaez/2f160264163e07cbd4e11a8718281f4a to your computer and use it in GitHub Desktop.
Hello QML
import sys
import random
from PyQt5 import QtCore, QtGui, QtQuick
class MyStupidModel(QtCore.QObject):
@QtCore.pyqtSlot(result=str)
def greet(self):
friends = ("Rob", "Fabrice", "Eoin", "Baz", "Stefano",
"Cesar", "JP", "Matt", "Frieder", "everybody!")
return "Hello {}".format(random.choice(friends))
def main():
app = QtGui.QGuiApplication(sys.argv)
model = MyStupidModel()
view = QtQuick.QQuickView()
view.setSource(QtCore.QUrl("Hello.qml"))
view.rootContext().setContextProperty("pymodel", model)
view.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
import QtQuick 2.5
Item {
id: container
property alias cellColor: rectangle.color
signal clicked(color cellColor)
width: 40; height: 25
Rectangle {
id: rectangle
anchors.fill: parent
}
MouseArea {
anchors.fill: parent
onClicked: container.clicked(container.cellColor)
}
}
import QtQuick 2.5
Rectangle {
id: container
color: "slategray"
width: 300; height: 200
Behavior on color {
ColorAnimation {
duration: 1000
}
}
Text {
id: helloText
color: "white"
text: "Hello QML"
font.pointSize: 24; font.bold: true
anchors.centerIn: parent
MouseArea {
anchors.fill: parent
onClicked: helloText.text = pymodel.greet()
}
}
Grid {
rows: 2; columns: 2; spacing: 3
Cell { cellColor: "green"; onClicked: container.color = cellColor }
Cell { cellColor: "blue"; onClicked: container.color = cellColor }
Cell { cellColor: "steelblue"; onClicked: container.color = cellColor }
Cell { cellColor: "black"; onClicked: container.color = cellColor }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment