Qt和没有moc _ *。cpp文件 [英] Qt and no moc_*.cpp file

查看:1496
本文介绍了Qt和没有moc _ *。cpp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的qt4应用程序,并使我自己的对话框。
我子类化QDialog,在类声明块中插入Q_OBJECT宏,并且...

I'm developing a simple qt4 app and making my own dialog. I subclassed QDialog, inserted the Q_OBJECT macro in the class declaration block, and...

我得到[链接器错误]未定义的引用`vtable for MyDialog '和没有
moc_MyDialog.cpp由moc编译器生成。

I get [Linker error] undefined reference to `vtable for MyDialog' and there is no moc_MyDialog.cpp generated by the moc compiler.

我使用Qt 4.1.3在Windows XP和mingw。我从Qt提供的构建shell的构建过程。我使用qmake创建make文件,并使用make命令编译所有内容。

I am using Qt 4.1.3 on Windows XP and mingw. I followed the build process from the Qt-supplied build shell. I used qmake to create make files and compiled everything with a make command.

我有其他类分别QPwnButton和QObject,但是他们编译
OK-我找不到他们和破碎的之间的任何差异。

I have other classes that subclass QPushButton and QObject respectively, but they compile OK—I can't find any differences between them and the broken one.

在破碎的类中必须有一些东西,但我无法发现它。 :(

There must be missing something in the broken class, but I'm unable to spot it. :(

帮助!

推荐答案

for MyDialog是因为没有moc文件,大多数c ++编译器在包含第一个虚函数的目标文件中创建vtable定义。当子类化一个qt对象并使用Q_OBJECT宏时,这将在moc * .cpp文件。

The undefined reference to "vtable for MyDialog" is caused because there is no moc file. Most c++ compilers create the vtable definition in the object file containing the first virtual function. When subclassing a qt object and using the Q_OBJECT macro, this will be in the moc*.cpp file. Therefore, this error means that the moc file is missing.

我可能想到的问题是:


  1. 类MyDialog.h的头文件未添加到qmake文件中的HEADERS。

  1. The header file for the class MyDialog.h is not added to HEADERS in the qmake file.

在添加Q_OBJECT宏之前运行qmake生成make文件,这样就创建了一个没有moc规则的make文件,只需简单地运行qmake就可以很容易地修复它。

You ran qmake to generate the make file before adding the Q_OBJECT macro. This created a make file without the moc rules. This is easily fixed by simply running qmake again.

您的对话框来自多个类,QDialog不是它派生的第一个类。要使qmake正常工作,QObject派生的基类需要是继承的第一个类。

Your dialog derives from more than one class and QDialog is not the first class that it derives from. For qmake to work correctly, the QObject derived base class needs to be the first class that is inherited from.

如果您使用的是Qt Creator,那么如果您之前的部署由于某种原因(如应用程序已在运行)失败,您可能会收到此错误。在这种情况下,只需执行清理项目,然后选择重建项目,然后选择运行即可部署。

If you are using Qt Creator, you might get this error if your previous deployment was failed due to some reason (like application already running). In that case, simply do a 'Clean Project' and then 'Rebuild Project' and then 'Run' to deploy.

这篇关于Qt和没有moc _ *。cpp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆