QML and Qt Quick
2019.09.29 16:14

QML내에서의 시그널, 슬롯 시스템

조회 수 11747 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

Qt는 기본적으로 QObject를 기반으로 시그널, 슬롯 시스템으로 동작된다. 이 것은 QML에서도 마찬가지이며 간단한 예제들을 통해 QML내에서 어떻게 사용되는지 알아보자.

 
기본 시그널
모든 qml 아이템은 다음과 같은 기본 시그널들이 있다.
 
completed()
객체가 생성될 때 발생
 
destruction()
객체가 파괴될 때 발생
 
간단한 예제를 보자.
SignalItem.qml
import QtQuick 2.12

QtObject {

}

 

main.qml

import QtQuick 2.13
import QtQuick.Window 2.13

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

    // Completed 시그널은 qml객체가 인스턴스화 되면 가장 먼저 발생하며 모든 qml에서 사용 할 수 있다. 
    Component.onCompleted: {
        console.log("Window created.")
    }

    SignalItem{
        id: mySignalItem
        Component.onCompleted: {
            console.log("SignalItem created.")
        }
    }
}

 

결과:

qml: Window created.
qml: SignalItem created.

 

사용자정의 시그널
새로운 시그널을 정의해서 다음과 같이 사용할 수도 있다.
 
SignalItem.qml
import QtQuick 2.12

QtObject {

    // 사용자정의 시그널
    signal signalItemCreated(string name)

    Component.onCompleted: {
        signalItemCreated("SignalItem")
    }
}

 

main.qml

import QtQuick 2.13
import QtQuick.Window 2.13

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

    Component.onCompleted: {
        console.log("Window created.")
    }

    SignalItem{
        id: mySignalItem
        onSignalItemCreated: {
            console.log(name + " created.")
        }
    }
}

 

결과:
qml: Window created.
qml: SignalItem created.

 

서로 다른 객체간 시그널과 슬롯을 연결

위의 예제에서는 시그널을 해당객체내에서 처리하였다. 이번에는 QML내에서 객체간 시그널 및 슬롯을 연결하는 방법을 소개한다.

 

QML내에서 객체간 시그널 슬롯을 연결하는 일반적인 방법은 Connections 요소를 사용하는 것이다. 

대상(객체id)을 설정후 다음과 같이 시그널이 발생할 때 시그널을 처리하는 "on<Signal이름>"핸들러를 작성한다.

 

main.qml

import QtQuick 2.13
import QtQuick.Window 2.13

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

    Component.onCompleted: {
        console.log("Window created.")
    }

    Connections{
        target: mySignalItem

        onSignalItemCreated:{
            console.log("SignalItem created.")
        }
    }

    SignalItem{
        id: mySignalItem
    }
}

 

결과:
qml: Window created.
qml: SignalItem created.

 

다만 Connections 의 사용에는 몇가지 제약이 있는데 예를들어 하나의 Connections에 같은 시그널 처리 함수를 여러개 만들 수 없고 대상(target) 객체가 사용되는 범위내에서만 사용할 수 있다. (물론 전역 객체의 시그널에 연결하는 건 가능하다.)

 

이번엔 조금더 C++ 스러운 방법을 알아보자.

C++에서 QObject의 connect, disconnect 메서드를 QML에서도 그대로 사용하여 서로 다른 객체간 시그널과 슬롯을 연결 할 수 있다.

 

main.qml

import QtQuick 2.13
import QtQuick.Window 2.13

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

    Component.onCompleted: {
        console.log("Window created.")

        // SignalItem의 시그널을 Window에 정의한 슬롯에 연결.
        mySignalItem.signalItemCreated.connect(signalItemCreatedHandler)
    }

    function signalItemCreatedHandler(){
        console.log("SignalItem created.")
    }

    SignalItem{
        id: mySignalItem
    }
}

 

결과:
qml: Window created.
qml: SignalItem created.

 


  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views131078
    read more
  2. Qt의 오픈소스 라이센스 소개

    Date2019.12.15 CategoryGeneral and Desktop Bymakersweb Views20447
    Read More
  3. No Image

    Qt for MCU 1.0 릴리즈

    Date2019.12.10 CategoryMobile and Embedded Bymakersweb Views8850
    Read More
  4. No Image

    Qt Marketplace 발표

    Date2019.12.02 CategoryGeneral and Desktop Bymakersweb Views5597
    Read More
  5. No Image

    QScopedPointer 소개 및 사용법

    Date2019.11.29 CategoryC++ Class Bymakersweb Views6318
    Read More
  6. QOpenVirtualkeyboard(Qt 5용 한글 및 영문, 숫자 가상키보드)

    Date2019.11.27 CategoryGeneral and Desktop Bymakersweb Views6836
    Read More
  7. Qt3D의 QML 타입으로 3D렌더링

    Date2019.11.20 CategoryQML and Qt Quick Bymakersweb Views6977
    Read More
  8. No Image

    라즈베리파이3에서 Boot to Qt 실행해보기

    Date2019.11.13 CategoryMobile and Embedded Bymakersweb Views8568
    Read More
  9. No Image

    Qt애플리케이션 객체(QCoreApplication, QGuiApplication, QApplication) 에 대해서

    Date2019.11.11 CategoryC++ Class Bymakersweb Views16854
    Read More
  10. No Image

    Qt Quick 3D 소개

    Date2019.11.09 CategoryQML and Qt Quick Bymakersweb Views7287
    Read More
  11. QPushButton 의 커스텀 이미지버튼

    Date2019.11.05 CategoryC++ Class Bymakersweb Views14378
    Read More
  12. qbs 사용 방법(Helloworld)

    Date2019.10.23 CategoryInstallation and Deployment Bymakersweb Views7676
    Read More
  13. 웹기반 Qt Design Viewer

    Date2019.10.23 CategoryQML and Qt Quick Bymakersweb Views7993
    Read More
  14. Qt Creator에서 Qt의존성 라이브러리 자동복사하기

    Date2019.10.19 CategoryInstallation and Deployment Bymakersweb Views11182
    Read More
  15. No Image

    Qt for Embedded Linux 화면출력

    Date2019.10.17 CategoryMobile and Embedded Bymakersweb Views6105
    Read More
  16. Windows에서 Qt 설치 따라하기

    Date2019.10.14 CategoryInstallation and Deployment Bymakersweb Views39726
    Read More
  17. Qbs 프로젝트를 정의하기 위해 사용되는 몇가지 중요한 아이템들

    Date2019.10.13 CategoryInstallation and Deployment Bymakersweb Views8586
    Read More
  18. No Image

    많은 리소스를 사용하는 Qt프로젝트에서 고려해봐야 할 qmake 옵션

    Date2019.10.11 CategoryGeneral and Desktop Bymakersweb Views8815
    Read More
  19. No Image

    Qbs에 대한 소개와 설치하는 방법

    Date2019.10.09 CategoryInstallation and Deployment Bymakersweb Views7752
    Read More
  20. OpenGL 렌더링을 QtQuick과 통합하는 방법

    Date2019.10.01 CategoryQML and Qt Quick Bymakersweb Views7908
    Read More
  21. No Image

    QML내에서의 시그널, 슬롯 시스템

    Date2019.09.29 CategoryQML and Qt Quick Bymakersweb Views11747
    Read More
Board Pagination Prev 1 3 4 5 6 7 9 Next
/ 9