Playing with coroutines and QML
May 29, 2018 by Jesús Fernández | Comments
** aka Playing with coroutines and Qt (Part II)
From 5747a7530206ac410b6bd7c1b8490d7d389ad3a5 JavaScript Generators are supported in QML. This enables to write the Fibonacci example from the previous post using generators and QML.
Mandatory example:
import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 2.2
Window {
property var fibonacci: function* () {
yield "0: 0"
yield "1: 1"
var f0 = 1, f1 = 0, n = 2;
while (true) {
var next = f1 + f0;
f0 = f1;
f1 = next;
yield n++ + ": " + (f0 + f1);
}
}
visible: true
width: 640
height: 480
title: qsTr("Fibonacci")
Row {
anchors.fill: parent
Button {
id: button
property var coroutine: fibonacci()
width: parent.width / 2; height: parent.height
text: coroutine.next().value
onPressed: text = coroutine.next().value
}
Button {
text: "Reset!"
width: parent.width / 2; height: parent.height
onPressed: {
button.coroutine = fibonacci()
button.text = button.coroutine.next().value
}
}
}
}
Have fun!
Blog Topics:
Comments
Subscribe to our newsletter
Subscribe Newsletter
Try Qt 6.5 Now!
Download the latest release here: www.qt.io/download.
Qt 6.5 is the latest Long-Term-Support release with all you need for C++ cross-platform app development.
Explore Qt World
Check our Qt demos and case studies in the virtual Qt World
We're Hiring
Check out all our open positions here and follow us on Instagram to see what it's like to be #QtPeople.
Näytä tämä julkaisu Instagramissa.Henkilön Qt (@theqtcompany) jakama julkaisu