Thursday, September 22, 2011

Qt Object Model :
Signals and slots for object communication
Object Properties 
Events and Even filters
Internationalization via extended character supports
Timers
Object trees
Guarded pointers
Object Cast
Thread support

Meta Object System :
Provides Qt functionalities : signals and slots mechanism, RTTI and dynamic property system.
MOS implementation is based on QObject class, Q_OBJECT macro and MOC.

MOC : Meta Object Compiler :
MOC transform Qt-C++ to plain C++.
MOC reads header file with Q_OBJECT macro in its private and produce moc_headername.cpp.

What does a MOC file contain ?
MOC file ( moc_headername.cpp ) contains meta object code to implement signals & slots, RTTI, and the dynamic property system. Such as the definition of functions like qt_metacall, qt_metacast, signals etc. 

What do Q_OBJECT do?
Q_OBJECT macro adds virtual functions declarations like that of qt_metacall(),qt_metacast(), metaObject() and staticMetaObject variable, which are later defined by the MOC.
qobject_cast will fail if a QObject subclass doesn't contain Q_OBJECT.
Q_OBJECT also defines tr() for translation.

How is a slot called when a signal is emitted ?
The definition of signal from the mocfile is executed in which QMetaObject::activate()is called with the signal’s name and arguments and also fetches the names for the appropriate slots that are connected.
Further, qt_metacall() with the slot names and the arguments is called. Then the desired slot (function) is called from a switch statement inside qt_metacall declaration in mocfile.

What do q_metacast() do ?
checks whether an object belongs to certain class and if it does returns the pointer to it. If it doesn’t, it returns 0.

QObject Class
QObject class is the base class of all Qt objects. It is the core of Qt object model.
Provides functions like connect or disconnect signals & slots.
Auto connection (run time connection using slot name or from UI file).
Supports Casting , timer, events, Object tree, thread , internationalization etc 
Unique identity (No copy constructors or assignment operators)

QApplication  
Class  manages the GUI application's control flow and main settings.QApplication contains the main event loop, where all events from the window system and other sources are processed and dispatched. (QCoreApplication for non-gui applications). QApplication is also responsible for application's initialization, finalization, session management and system wide settings such as colour, style ,cursor, font etc.

What happens when there are two QApplication instances are running ?
QApplication follows singleton pattern. However we can run instances in different process.But, it will be waste of resources as each QApplication instance does a lot of initializations.

What is qApp pointer ?
A global pointer referring to the unique application object.

Advantage of qobject_cast over dynamic_cast
qobject_cast doesn't require RTTI support and it works across dynamic library boundaries.

SLOT, SIGNAL and emit Macros
# define emit
emit expands to nothing! It just increases the readability.SLOT and SIGNAL macros expands to "1" and "2". This is to distinguish between slots and signals.
# define SLOT(a)     "1"#a
# define SIGNAL(a)   "2"#a

Macros / Qt keywords : signals, slots, Q_SIGNALS and Q_SLOTS
#define slots 
#define signals protected 
#define Q_SLOTS
#define Q_SIGNALS protected
slots and Q_SLOTS expands to nothing. Signals and Q_SIGNALS expands to protected.