Stay up to date with the latest marketing, sales and service tips and news.
この記事は The Qt Blog の Playing with coroutines and QML を翻訳したものです。
執筆: Jesús Fernández, 2018年05月29日
(別名 コルーチンと Qt で遊んでみた パート2)
5747a7530206ac410b6bd7c1b8490d7d389ad3a5 により JavaScript Generators のサポートが QML に追加されました。これによって、前回の記事のフィボナッチ数列もジェネレーターで書けるようになりました。
サンプルコード:
import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 2.2Window {
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
}
}
}
}
素敵ですね!
Stay up to date with the latest marketing, sales and service tips and news.
Download the latest release here: www.qt.io/download.
Qt 5.12 was developed with a strong focus on quality and is a long-term-supported (LTS) release that will be supported for 3 years.
Find webinars, use cases, tutorials, videos & more at resources.qt.io
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