한국어
Qt
 

QML and Qt Quick QML과 코루틴(Coroutines)

makersweb 2020.11.03 22:25 조회 수 : 577

function * 선언 (함수 키워드 뒤에 별표)은 Generator 객체를 반환하는 generator 함수를 정의한다. 다음의 피보나치 예제는 generator와 QML을 사용하여 코루틴의 예를 보여준다.

import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Fibonacci")

    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);
        }
    }

    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
            }
        }
    }
}

 

코루틴 이란?

코루틴(coroutine)은 루틴의 일종으로서, 협동 루틴이라 할 수 있다(코루틴의 "Co"는 with 또는 togather를 뜻한다). 상호 연계 프로그램을 일컫는다고도 표현가능하다. 루틴과 서브 루틴은 서로 비대칭적인 관계이지만, 코루틴들은 완전히 대칭적인, 즉 서로가 서로를 호출하는 관계이다. 코루틴들에서는 무엇이 무엇의 서브루틴인지를 구분하는 것이 불가능하다. 코루틴 A와 B가 있다고 할 때, A를 프로그래밍 할 때는 B를 A의 서브루틴으로 생각한다. 그러나 B를 프로그래밍할 때는 A가 B의 서브루틴이라고 생각한다. 어떠한 코루틴이 발동될 때 마다 해당 코루틴은 이전에 자신의 실행이 마지막으로 중단되었던 지점 다음의 장소에서 실행을 재개한다. 출처: 위키백과

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 85857
39 Qt Bluetooth 관련 기능 확인 사항 makersweb 2018.07.10 763
38 Q_D매크로와 d-pointer file makersweb 2019.05.07 759
37 Qt for MCU 1.0 릴리즈 makersweb 2019.12.10 751
36 싱글 샷(Single-Shot) 시그널/슬롯 연결 makersweb 2021.05.12 751
35 2020년에 변경되는 Qt 오퍼 (Qt offering changes 2020) [2] j2doll 2020.01.31 720
34 단일 인스턴스 Qt 응용 프로그램(Single-instance Application) makersweb 2022.06.23 700
33 Qt 응용프로그램에서 PDF 문서 렌더링 file makersweb 2021.09.23 677
32 Qt5Compat 라이브러리를 사용하여 Qt5에서 Qt6로 포팅 [2] makersweb 2020.12.05 672
31 Qt Bluetooth Low Energy 개요 makersweb 2022.02.13 661
30 QML에서 D-Bus 통신 file makersweb 2023.03.15 647
29 클라우드용 Qt file makersweb 2024.01.16 636
28 Qt Marketplace 발표 makersweb 2019.12.02 617
27 Embedded Linux 에서 Qt 및 Graphics Stack file 운영자 2020.05.27 612
26 QProcess 보안 권고 리뷰 file makersweb 2022.09.18 597
25 최초의 Qt 6.0 스냅샷 제공 (First Qt 6.0 Snapshot Available) j2doll 2020.06.21 591
» QML과 코루틴(Coroutines) makersweb 2020.11.03 577
23 clazy 로 13개의 시그널, 슬롯 오류 해결 makersweb 2022.08.23 566
22 응용프로그램 자동실행 설정 (on Windows) makersweb 2021.05.08 563
21 OpacityMask 예제 file makersweb 2023.01.26 562
20 Qt Safe Renderer 개요 file makersweb 2022.09.08 541