Q_OBJECT 란

QT 2018. 2. 28. 11:12

Q_OBJECT


The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.

http://doc.qt.io/qt-5/qobject.html#Q_OBJECT


Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.

http://doc.qt.io/qt-5/qobject.html - Detailed Description


Q_OBJECT는 siganl이나 slot 등 Qt의 meta-object system을 사용하려는 클래스에서 private 영역에 선언해야 하는 매크로이다. 라고 한다. 그리고 만약 위의 기능을 사용하지 않는다고 해도 이 매크로를 사용하는 것을 권장하고 있다.




The Meta-Object System


그렇다면 여기서 meta-object system이란 무엇일까


Qt에서의 meta-object system은 object 간의 통신 메카니즘을 위한 signal과 slot, 런타임 타입 정보, 그리고 동적 property 시스템을 제공한다.


그리고 아래 세 가지에 베이스를 두고 있다.

1. QObject 클래스 meta-object system을 이용할 수 있는 오브젝트에 대한 base 클래스를 제공한다.

2. 클래스의 private 섹션에 선언된 Q_OBJECT 매크로는 dynamic properties, signal, slot 등과 같은 meta-object의 기능을 사용할 수 있게 한다.

3. Meta-Object Compiler(moc)는 QObject의 subclass에게 meta-object 기능 구현에 필요한 코드를 지원한다.


moc 툴은 C++ 소스 파일을 읽어 Q_OBJECT 매크로가 선언된 클래스를 찾고, meta-object 코드가 포함된 소스를 생성한다. 이 생성된 코드는 클래스의 소스 파일에서 include 하는데, 일반적으로 클래스 구현과 함께 컴파일 및 링크가 된다.



원문을 보고 해석했는데, 이게 맞는지 잘 모르겠다.


나중에 정확히 이해하면 다시 정리해봐야겠다...




참고


http://doc.qt.io/qt-5/metaobjects.html

블로그 이미지

NCookie

,