한국어
Qt
 

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

makersweb 2020.11.03 22:25 조회 수 : 4566

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 118358
180 Qt Android 앱에 AdMob 배너달기 file makersweb 2021.12.04 3208
179 HTTPS URL을 연결할 때 SslHandshakeFailedError 오류 makersweb 2022.07.31 3338
178 Qt 6.4에 추가될 Qt Quick 3D Physics file makersweb 2022.08.07 3444
177 성능 고려 및 제안 사항 makersweb 2022.03.07 3511
176 QProcess 보안 권고 리뷰 file makersweb 2022.09.18 3572
175 Binding 타입으로 객체 속성 간 묶기 makersweb 2022.03.04 3837
174 그래픽 소프트웨어에서 디자인 내보내기 (Exporting Designs from Graphics Software) j2doll 2020.12.25 3849
173 Qt Bluetooth Low Energy 개요 makersweb 2022.02.13 3948
172 QML에서 Websocket 서버와 통신 file makersweb 2021.09.18 4006
171 Qt Marketplace 발표 makersweb 2019.12.02 4042
170 응용프로그램 자동실행 설정 (on Windows) makersweb 2021.05.08 4082
169 VirtualKeyboard 스타일 커스터 마이징 makersweb 2022.03.13 4109
168 Qt 6 에서 프로퍼티 바인딩 makersweb 2021.04.03 4110
167 Qt5Compat 라이브러리를 사용하여 Qt5에서 Qt6로 포팅 [2] makersweb 2020.12.05 4197
166 QML 에서 QR코드 생성 file makersweb 2021.08.20 4200
165 Qt 응용프로그램에서 Lottie Animation사용 file makersweb 2021.05.30 4221
164 Qt 응용프로그램에서 PDF 문서 렌더링 file makersweb 2021.09.23 4258
163 QtQuick 애플리케이션에 Rive 애니메이션 통합 makersweb 2024.03.31 4272
162 Qt Safe Renderer 개요 file makersweb 2022.09.08 4285
161 QMake 프로젝트를 CMake 프로젝트로 변환 with qmake2cmake makersweb 2022.09.17 4347