Qt - moc 文件相关错误

Qt - moc file related error

本文关键字:错误 文件 moc Qt      更新时间:2023-10-16

我正在编写一个项目。在尝试进行静态构建后,我开始遇到错误。我做了一些更改,我不记得了。但是,我敢肯定,如果可以清除此存根,那么也可以清除主项目。

这是头文件。

#ifndef MYLABEL_H
#define MYLABEL_H
#include <QFileDialog>
#include <QLabel>
#include <QMouseEvent>
#include <QObject>
#include <QPaintEvent>
class MyLabel:public QWidget
{
private:
    QPixmap default_Pixmap;
    QPixmap pixmap;
    QFileDialog * fileDialog;
    Q_OBJECT
public:
    MyLabel();
    void setPixmap(QPixmap pixmap);
    void setDefault();
protected:
    void mousePressEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent * event);
signals:
    void file_Selected(QString fileName);
private slots:
    void file_Got_Selected(QString fileName);
};
#endif // MYLABEL_H

这是源文件

#include "MyLabel.h"
#include "MyMessageBox.h"
#include <QFileDialog>
#include <QPainter>
MyLabel::MyLabel():QWidget()
{
    default_Pixmap = QPixmap("select.gif").scaled(250,100);
    this->fileDialog=new QFileDialog(this);
    fileDialog->setNameFilter("Image Files (*.BMP *.GIF *.JPG *.JPEG *.PNG *.PBM *.PGM *.PPM *.XBM *.XPM)");
    connect(fileDialog,SIGNAL(fileSelected(QString)),this,SIGNAL(file_Selected(QString)));
    connect(fileDialog,SIGNAL(fileSelected(QString)),this,SLOT(file_Got_Selected(QString)));
}
void MyLabel::setPixmap(QPixmap pixmap)
{
    this->pixmap = pixmap;
}
void MyLabel::setDefault()
{
    this->pixmap = default_Pixmap;
}
void MyLabel::mousePressEvent(QMouseEvent *event)
{
    fileDialog->show();
    //QString file_Name = file_Dialog.getOpenFileName();
}
void MyLabel::paintEvent(QPaintEvent * event)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,width(),height(),pixmap.scaled(QSize(width(),height())));
}
void MyLabel::file_Got_Selected(QString fileName)
{
    this->pixmap = QPixmap(fileName);
}
#include "myLabel.moc"

这是主文件

#include <QLabel>
#include <QPixmap>
#include <QtGui/QApplication>
#include "myLabel.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyLabel mm;
    //mm.setPixmap(QPixmap("dummy.jpg"));
    //mm.setPixmap(QPixmap());
    mm.setDefault();
    mm.show();
    return a.exec();
}

我使用 qt 命令提示符和命令创建了 moc 文件

moc myLabel.h -o myLabel.moc

在此之后,我尝试通过Qt-Editor编译项目。但是我得到了如下多定义错误,

debug/moc_myLabel.o:d:/TempInstallationFolder/Qt/Dynamic/qt/include/QtCore/../../src/corelib/global/qglobal.h:1381: multiple definition of `MyLabel::metaObject() const'

debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:57:首先在这里定义

debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:62: 'MyLabel::qt_metacast(char const*)' 的多重定义

debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:62:首先在这里定义

debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:70: 'MyLabel::qt_metacall(QMetaObject::Call, int, void**)' 的多重定义

debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:70:首先在此处定义

debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:87: 'MyLabel::file_Selected(QString)' 的多重定义

debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:87:首先在这里定义

debug/moc_myLabel.o:moc_myLabel.cpp:(.data+0x0):'MyLabel::staticMetaObject' 的多重定义

debug/myLabel.o:myLabel.cpp:(.data+0x0):首先在这里定义

收集2:LD 返回 1 个退出状态

mingw32-make[1]: * [调试\日历新.exe] 错误 1

mingw32-make: * [调试] 错误 2

进程"D:/TempInstallationFolder/Qt/Dynamic/mingw/bin/mingw32-make.exe"以代码 %2 退出。生成项目日历时出错 日历新建 (目标: 桌面)执行构建步骤"制作"时

任何人请让我摆脱这个问题。

尝试从源文件中删除include "MyLabel.moc"行。您无需将其包含在cpp文件中。