コルーチンと QML で遊んでみた
October 30, 2018 by 鈴木 佑 | Comments
この記事は 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
}
}
}
}
素敵ですね!
Blog Topics:
Comments
Subscribe to our newsletter
Subscribe Newsletter
Try Qt 6.4 Now!
Download the latest release here: www.qt.io/download.
Qt 6 is the productivity platform for the future, with next-gen 2D & 3D UX and limitless scalability.
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