`

[转]Moc的三种用法

    博客分类:
  • Qt
 
阅读更多
(原文地址:https://code.google.com/p/qextserialport/wiki/Three_Usage_Of_MOC)
Three_Usage_Of_MOC 
Updated Jan 30, 2013 by dbzhang...@gmail.com
All the developers of Qt should have been familiar with normal usage of MOC.

How to use moc(1)
Consider that we have a Qt project, which contains a subclass of QObject, and the definition of the class is in a header file.
// myclass.h
class MyClass : public QObject
{
Q_OBJECT
public:
     MyClass();
};
When qmake is running, once it find macro Q_OBJECT in header file, it will call moc for us.
moc myclass.h -o moc_myclass.cpp
As moc_myclass.cpp is a compile unit, so qmake will run compiler to compile it, then link the moc_myclass.o(bj) to other part. All of them are transparent for users.
g++ moc_myclass.cpp -o moc_myclass.o
How to use moc(2)
What will happen if the definition of the class located in one .cpp source file?
// main.cpp
class MyClass : public QObject
{
Q_OBJECT
public:
     MyClass(){}
};

int main(int argc, char**argv)
{
...
}
When qmake is running, once it find macro Q_OBJECT in source file, it will call moc for us too.
moc main.cpp -o main.moc
Then what's the difference?
main.moc is not a compile unit any, as it will dependent on the definition of class MyClass.So it can not include the file "main.cpp".
# This will fail ....
g++ main.moc -o main_moc.o
How to solve this?
//Add following line to main.cpp file, after the definition of MyClass
#include "main.moc"
How to use moc(3)
If you have read the source code of qextserialport, you will find that

QextSerialPort defined in the header file qextserialport.h
But qextserialport.cpp contains following line
#include "moc_qextserialport.cpp"
So, when qmake is running, once it find macro Q_OBJECT in header file, it will call moc for us.
moc myclass.h -o moc_myclass.cpp
But, why we include "moc_qextserialport.cpp" manully?
In order to move private data member and private slots to non-public files, d-pointer and Q_PRIVATE_SLOT are used.

So the generated moc_qextserialport.cpp is dependent on non-public files will make it can not be compiled as a compile unit.

# This doesn't work any more ;-)
g++ moc_qextserialport.cpp -o moc_qextserialport.o
why both moc_xxx.cpp and xxx.moc exist?
In same cases, both myclass.h and myclass.cpp may have Q_OBJECT

// myclass.h
class MyClass : public QObject
{
Q_OBJECT
public:
     MyClass();
};
// myclass.cpp
class MyClassHelper : public QObject
{
Q_OBJECT
public:
     MyClassHelper();
};

MyClass::MyClass()
{
}
...
In such cases, moc will generated two files for us

moc myclass.h -o moc_myclass.cpp
moc myclass.cpp -o myclass.moc
分享到:
评论

相关推荐

    Moc.zip_moc_moc4455_喷管 JAVA

    使用特征方法进行流过喷管的超音速流的二维分析的JAVA源程序

    Qt中第三方日志库qslog的基本配置和使用详解示例程序

    Qt中第三方日志库qslog的基本配置和使用详解示例程序,可参考文章:https://blog.csdn.net/hp_cpp/article/details/123660840

    最小长度喷嘴设计使用特性方法:使用MOC设计最小长度2d钟形喷嘴-matlab开发

    设计马赫数、膨胀波数、比热比由用户提供。 文件planar_moc.m 生成特征网络和喷嘴轮廓。

    QT_connect函数的用法

    信号和槽是 QT 自行定义的一种通信机制,它独立于标准的 C/C++ 语言,因此要正确的处理信号和槽,必须借助一个称为 moc(Meta Object Compiler)的 QT 工具,该工具是一个 C++ 预处理程序,它为高层次的事件处理自动...

    Excel库以及使用方法

    Qt是面向对象的框架,使用特殊的代码生成扩展(称为元对象编译器(Meta Object Compiler, moc))以及一些宏,Qt很容易扩展,并且允许真正地组件编程。2008年,Qt Company科技被诺基亚公司收购,Qt也因此成为诺基亚...

    基于Windows平台VxWorks交叉编译工具的设计

    开发厂商会推出一整套交叉编译工具链来配合自身的嵌入式产品,但是这些由商业公司提供的工具链,都不会附有工具链相关的源代码和制作方法,灵活性不足,并且它们一般都与整套开发系统捆绑销售使用,成本较高。

    C#实现翻转字符串的方法

    本文实例讲述了C#实现翻转字符串的方法。分享给大家供大家参考。具体实现方法如下: Func<string> ReverseString = delegate(string s) { char[] word = s.ToCharArray(); for (int i = 0, j = (s.Length - 1); i...

    引力波电磁优化_python_代码_下载

    引力波电磁优化 该代码目前可以: 与 Gracedb 交互,下载天空图,阅读它们等。 读取包含位置、FOV、限制幅度、曝光时间等的望远镜配置文件。 根据望远镜配置(MOC,...更多详情、使用方法,请下载后阅读README.md文件

    python对指定字符串逆序的6种方法(小结)

    对于一个给定的字符串,逆序输出,这个任务对于python来说是一种很简单的操作,毕竟强大的列表和字符串处理的一些列函数足以应付这些问题 了,今天总结了一下python中对于字符串的逆序输出的几种常用的方法 方法一:...

    如何对双向触发二极管进行检测

    如何对双向触发二极管进行检测,使用万用表检测双向触发二极管的方法简介

    QtMeatObjectEx.zip

    元对象系统moc(Meat-Object System)的对象MetaObject和(含动态)属性Propert的用法

    gobject-cpp11-master.zip

    使用方法类似Qt的信号槽,优点是不需要moc;基于c++11,用于解耦合;GOSP(GUI框架)以此信号槽为特色和基础; signal/slot which is Qt-Style, and not depends on moc

    stado:cmu11785项目

    通过将动作实例视为移动点的轨迹,提出一种新的动作细管检测框架,称为“ MoveCenterCenter检测器”(MOC-detector) 。 MOC检测器被分解为三个关键的头分支: (1)中心分支,例如中心检测和动作识别。 (2)运动...

    Giveda_GObject_c++11_v1.5

    为了解决Qt信号槽(需要依赖moc机制和moc工具)的缺陷,我创造了本软件。 利用本软件,c++开发者可以在不依赖c++...开发者可以将本软件放到开发环境中去编译和运行,并配合《用户文档.doc》来学习本软件的使用方法。

    VS2019中QT连接及使用的方法步骤

    在qt中可以直接添加信号与槽 在vs中使用是不一样的 直接新建一个qt 项目 打开 设计师界面 在vs中 的头文件中添加 槽函数 .cpp中实现 比较让人在意的是 构造函数中 ui.setupUi(this) 在qt中是ui->setupUi(this) 在...

    基于小程序实现小说阅读系统微信小程序源码带后端

    轮播有几种形式, 比如常见的横向海报图片展示, 还有横纵向商品列表展示,头条信息框轮换 siwper组件很好的实现了横向海报图片展示,比如 头条信息框转换采用上下轮换, 使用scroll-view嵌套 1. 微信小程序的组件...

    基于小程序实现小说阅读系统微信小程序源码带后端分享

    轮播有几种形式, 比如常见的横向海报图片展示, 还有横纵向商品列表展示,头条信息框轮换 siwper组件很好的实现了横向海报图片展示,比如 头条信息框转换采用上下轮换, 使用scroll-view嵌套 1. 微信小程序的组件...

    OpenNI获取的图像结合OpenCV显示

    不过本人在使用该种方法时kinect一直驱动不成功,即使用opencv的VideoCapture类来捕捉Kinect设备的数据,一直是打不开的,即驱动不成功。但是kinect设备已经连接上了,且能运行openni中的sample,说明kinect的硬件...

    论文研究 - 油轮航行审核制定桥梁检查方法的安全指示

    出于安全考虑,主要石油公司(MOC),港口国,船级社和船旗国增加了对油轮的检查次数。 由于密集的货运操作,油轮检查变得越来越困难和效率低下。 低效,主观或不合标准的检查/评估通常会导致错误的决定,并导致不...

    Qt5教程(带书签目录)

    Qt 是 1991 年由奇趣科技开发的跨平台 C++图形用户界面应用程序开发框架。... Qt 是面向对象的框架, 使用特殊的代码生成扩展(称为元对象编译器(Meta Object Compiler, moc)) 以及一些宏, 易于扩展, 允许组件编程。

Global site tag (gtag.js) - Google Analytics