Last active
October 5, 2017 17:16
-
-
Save csaez/2f160264163e07cbd4e11a8718281f4a to your computer and use it in GitHub Desktop.
Hello QML
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 | |
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() |
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 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) | |
} | |
} |
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 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