QT Meta data

Q_OBJECT

You use the Q_OBJECT macro to tell the compiler that this class uses own signals and slots. AFAIK Qt doesn’t use standard signal & slot concept and with that macro you need to run meta object compiler (moc) first —> it changes the code in “real” c++ code that is usefull for the g++ compiler. Hence, you write it in the classes you define to have own signals & slots and then you pre-compile it with moc and finally compile both (moc_file.cpp & file.cpp)… (I hope that is correct like this^^)

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. And to expand that a bit, for everything that inherits from QObject :)

对于QT的代码来说,任何使用到信号和槽机制的object,都需要在类定义开始之前加上Q_OBJECT的宏。

Q_PROPERTY

Properties are parts of Qt’s Meta Object system: once you declare a Q_PROPERTY, it is available for invocation through MOC (QObject::setProperty(), QObject::property(), and indirectly by QObject::invokeMethod()). QProperties are widely used by in QML. In fact they are a crucial, basic part of QML.

Q_ASSERT

  1. void Q_ASSERT(bool test)

Prints a warning message containing the source code file name and line number if test is false.
Q_ASSERT() is useful for testing pre- and post-conditions during development. It does nothing if QT_NO_DEBUG was defined during compilation.
QT的断言语句,当开关QT_NO_DEBUG打开时,该宏不起作用。

  1. // File: div.cpp
  2. #include <QtGlobal>
  3. int divide(int a, int b)
  4. {
  5. Q_ASSERT(b != 0);
  6. return a / b;
  7. }
  8. //If b is zero
  9. ASSERT: "b != 0" in file div.cpp, line 7

QMetaObject Struct

The QMetaObject class contains meta-information about Qt objects.
Header: #include

The Qt Meta-Object System in Qt is responsible for the signals and slots inter-object communication mechanism, runtime type information, and the Qt property system. A single QMetaObject instance is created for each QObject subclass that is used in an application, and this instance stores all the meta-information for the QObject subclass. This object is available as QObject::metaObject().
This class is not normally required for application programming, but it is useful if you write meta-applications, such as scripting engines or GUI builders.