Qt Meta Object Compiler Installing
Download File > https://byltly.com/2tuCO2
How to Install Qt Meta Object Compiler on Windows 10
Qt Meta Object Compiler (moc) is a tool that handles Qt's C++ extensions, such as signals and slots, properties, and enums. It generates C++ source code from header files that contain the Q_OBJECT macro. To use moc, you need to have a compatible compiler and Qt version installed on your Windows 10 system.
In this article, we will show you how to install moc and use it with Qt 4.8.7 and Microsoft Visual C++ 2008 Express Edition. These are the steps:
Download and install Qt 4.8.7 from here. Make sure you select the option to install the Qt libraries for Desktop.
Download and install Microsoft Visual C++ 2008 Express Edition from here. This is a free version of the compiler that works with Qt 4.8.7.
Open Qt Creator, the integrated development environment (IDE) for Qt. You can find it in the Start menu under Qt by Digia.
Go to Tools > Options > Build & Run > Qt Versions and click on Add. Browse to the bin folder of your Qt installation (e.g., C:\\Qt\\4.8.7\\bin) and select qmake.exe. Give it a name, such as Qt 4.8.7.
Go to Tools > Options > Build & Run > Compilers and click on Add > Microsoft Visual C++. Browse to the bin folder of your Visual C++ installation (e.g., C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\bin) and select cl.exe. Give it a name, such as MSVC 2008.
Go to Tools > Options > Build & Run > Kits and click on Add. Select your Qt 4.8.7 version and the MS compilers for C and C++, your favorite debugger and input the Qt-makespec win32-msvc2008. Give it a name, such as Qt 4.8.7 MSVC 2008.
Press OK to save your settings. Now you should be able to create a new project or open an existing one that uses moc.
To use moc, you need to include the Q_OBJECT macro in your class declaration and add the generated source file (e.g., moc_myclass.cpp) to your project file (e.g., myproject.pro). For example:
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(QObject *parent = 0);
MyClass();
signals:
void mySignal();
public slots:
void mySlot();
};
// in myproject.pro
SOURCES += main.cpp \\
myclass.cpp \\
moc_myclass.cpp
Build and run your project as usual. You should see the meta-object code for your class in the moc_myclass.cpp file.
We hope this article was helpful for you. If you have any questions or feedback, please leave a comment below.
In this section, we will show you some advanced features of moc that you can use to enhance your Qt applications.
Using Q_ENUM and Q_FLAG
Q_ENUM and Q_FLAG are macros that declare enums and flags that can be used with the meta-object system. For example, you can use them to define properties of enum or flag type, or to expose them to QML. To use them, you need to include them in your class declaration after the enum or flag definition. For example:
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
Q_ENUMS(Priority)
Q_FLAGS(Options)
public:
enum Priority { High, Low, VeryHigh, VeryLow };
enum Option { Option1, Option2, Option3 };
Q_DECLARE_FLAGS(Options, Option)
MyClass(QObject *parent = 0);
MyClass();
void setPriority(Priority priority);
Priority priority() const;
void setOptions(Options options);
Options options() const;
signals:
void priorityChanged(Priority priority);
};
// in myproject.pro
SOURCES += main.cpp \\
myclass.cpp \\
moc_myclass.cpp
By using Q_ENUM and Q_FLAG, you can access the enum and flag values by name from the meta-object system. For example, you can use QVariant::fromValue () and QVariant::value () to convert between enum/flag values and QVariant. You can also use QMetaEnum::fromType () and QMetaEnum::key () to get the string representation of an enum/flag value. For example:
MyClass *obj = new MyClass();
QVariant v = QVariant::fromValue(obj->priority()); // v is a QVariant of type Priority
obj->setPriority(v.value()); // converts v back to Priority and sets it
QMetaEnum me = QMetaEnum::fromType(); // gets the meta-enum for Priority
qDebug() priority()); // prints the name of the current priority value
You can also use Q_ENUM and Q_FLAG to expose your enums and flags to QML. For example, you can register your class as a QML type and then use its enums and flags in your QML code. For example:
// in main.cpp
#include \"myclass.h\"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType(\"MyApp\", 1, 0, \"MyClass\");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral(\"qrc:/main.qml\")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
// in main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import MyApp 1.0
Window {
visible: true
width: 640
height: 480
MyClass {
id: obj
priority: MyClass.VeryHigh // use enum value by name
options: MyClass.Option1 MyClass.Option3 // use flag values by name
onPriorityChanged: console.log(\"Priority changed to \" + priority) // use enum value as is
}
Text {
text: \"Priority: \" + obj.priority + \"\\nOptions: \" + obj.options // use enum and flag values as is
anchors.centerIn: parent
}
}
Using Q_CLASSINFO
Q_CLASSINFO is a macro that allows you to attach additional name/value pairs to the class's meta-object. You can use it to provide extra information about your class that is not part of the standard meta-object features. For example, you can use it to specify the author or status of your class. To use it, you need to include it in your class declaration with a name and a value as strings. For example:
class MyClass : public QObject
{
Q_OBJECT
Q_CLASSINFO(\"Author\", \"John Doe\")
Q_CLASSINFO(\"Status\", \"Active\")
public:
MyClass(QObject *parent = 0);
MyClass();
};
By using Q_CLASSINFO, you can access the name/value pairs by using the ec8f644aee