qt: undefined reference to `vtable for aaa‘

版本qt4.8.6,编译报错"main.cpp:(.text+0x3b): undefined reference to `vtable for aaa'"

就一个main.cpp

cpp 复制代码
#include <QApplication>
#include <QTimer>
#include <QCursor>
#include <QMouseEvent>
#include <QDesktopWidget>
#include <QDebug>
#include <qglobal.h>

class aaa:public QApplication
{
    Q_OBJECT
public:
    aaa(int argc, char *argv[]):QApplication(argc,argv){}
private slots:
    void moveMouse()
    {
        QPoint point(100, 100); // 设置鼠标移动的目标位置
        QMouseEvent mouseEvent(QEvent::MouseMove, getRandomPosition(), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
        QApplication::sendEvent(QApplication::desktop(), &mouseEvent);
        qDebug() << "Mouse moved to" << getRandomPosition();
    }
    QPoint getRandomPosition() {
    int screenWidth = QDesktopWidget().width();
    int screenHeight = QDesktopWidget().height();
    return QPoint(qrand()%screenWidth, qrand()%screenHeight);
}
};
int main(int argc, char *argv[]) {
    aaa app(argc, argv);
    QTimer timer;
    QObject::connect(&timer, SIGNAL(timeout()), &app, SLOT(moveMouse()));
    timer.start(1000*60*10);
    return  app.exec();
}
#include "main.moc"   //这一句很关键

由于类aaa使用了信号槽,并且写在源文件中,致使找不到main.moc,需要在文件末尾主动包含"main.moc"

使用 #include "main.moc" 或者类似的包含 .moc 文件的情况,通常出现在以下两种情况:

  1. 当你在.cpp源文件中定义了一个Q_OBJECT宏的类时。
  2. 当你手动调用 moc 命令来处理一个头文件,并将生成的源文件包含到你的项目中时。

如果你没有在 .cpp 文件中定义 Q_OBJECT 宏的类,并且没有手动使用 moc,那么你不需要包含 .moc 文件。通常,在用 qmakeCMake 等构建系统生成的项目中,.moc 文件会自动被处理和包含,不需要你手动包含。

为了避免以上问题出现,如果使用率Q_OBJECT宏,该类必须在头文件声明

相关推荐
程序员三藏几秒前
Selenium三大等待
自动化测试·软件测试·数据库·python·selenium·测试工具·测试用例
闪电麦坤9512 分钟前
SQL:Constraint(约束)
数据库·sql
Miraitowa_cheems13 分钟前
JAVA SE 自我总结
java·开发语言·javase
码猩22 分钟前
C# winform根据EXCEL匹配文件后将txt的图片分别下载到指定的文件夹里
开发语言·c#·excel
Alt.928 分钟前
SpringMVC基础三(json)
java·开发语言
搬砖工程师Cola28 分钟前
<C#>在 C# .NET 中,使用 LoggerExtensions方法创建日志
开发语言·c#·.net
观无39 分钟前
.NET-EFCore基础知识
数据库·.net
^_^ 纵歌42 分钟前
mongodb和clickhouse比较
数据库·clickhouse·mongodb
小学生搞程序44 分钟前
学习Python的优势体现在哪些方面?
开发语言·python·学习
yezipi耶不耶44 分钟前
Rust入门之迭代器(Iterators)
开发语言·后端·rust