Qt - moc文件相关的错误

时间:2011-05-08 06:46:49

标签: c++ qt user-interface logic

我正在编写一个项目。在我尝试进行静态构建后,我开始出错。我做了一些改变,我不记得了。但是,我确信如果可以清除此存根,那么主项目也可以被清除。

这是头文件。

#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,无效**)'

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):首先在这里定义

collect2:ld返回1退出状态

mingw32-make [1]: * [debug \ CalendarNew.exe]错误1

mingw32-make: * [debug]错误2

进程“D:/TempInstallationFolder/Qt/Dynamic/mingw/bin/mingw32-make.exe”退出,代码为%2。 构建项目CalendarNew时出错(目标:桌面) 执行构建步骤'Make'时

任何人请解决这个问题。

1 个答案:

答案 0 :(得分:2)

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