DevSight

Qt/C++, Signals & Slots

QT의 핵심기능 중 하나인 Signal, Slot은 객체 간의 통신에 사용된다. 일반적인 개발언어의 객체 간 메시지 통신인 ‘메시지-메시지 핸들러’, ‘이벤트-이벤트 핸들러’와 유사한 개념이다.

signal과 slot을 연결하기 위해 connect()를 사용한다. connect를 사용하는 방법은 함수 포인터를 사용하거나 람다에 연결한다.

Singleton으로 확장한 예제는 여기(GitHub)에서 볼 수 있다.

connect() 함수 형식
1
2
connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed);
connect(sender, &QObject::destroyed, this, [=](){this->m_objects.remove(sender);});
Console 예제
1
2
3
4
5
6
7
8
9
10
//main.cpp
#include "water_cooler.h"
#include <QCoreApplication>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    WaterCooler Cooler;
    a.exit(0);
}
Read More ···