Jesús Fernández

Playing with coroutines and QML

Published Tuesday May 29th, 2018
6 Comments on Playing with coroutines and QML
Posted in Qt | Tags: ,

** 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
            onPressed: text =

        Button {
            text: "Reset!"
            width: parent.width / 2; height: parent.height
            onPressed: {
                button.coroutine = fibonacci()
                button.text =

Have fun!

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Qt | Tags: ,


jason says:

This is good progress, but I’m waiting for true async/await support and Node API compatibility for things like the core ‘fs’ module.

Lars Knoll Lars Knoll says:

That’ll take a bit more time. The current work is aimed towards getting ECMAScript 7 compliance. async/await is ES8, and we don’t be going there before we have ES7 fully supported.

Pavel Vasiliev says:

do it:
please, please, please! ))

Pavel Vasiliev says:

it doesn’t work for me ((
“property var fibonacci: function* () {”
error: Unexpected token “*”

Jesús Fernández Jesús Fernández says:

I am using the dev branch (aka 5.12).

Pavel Vasiliev says:

Thanks, I’ll try.

Commenting closed.

Get started today with Qt Download now