2011-05-08 53 views
2

我正在編寫一個項目。我嘗試進行靜態構建後,我開始出現錯誤。我做了一些我不記得的改變。但是,我相信如果這個存根可以被清除,那麼主項目也可以被清除。Qt - moc文件相關錯誤

這是頭文件。

#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編輯器進行項目。但我得到的多定義錯誤如下,

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

調試/ myLabel.o:C:\ Documents和Settings \普拉巴卡蘭\桌面\ CalendarNew - 構建 - 桌面/../ 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,無效**)」

調試/ myLabel.o:C:\ Documents和Settings \普拉巴卡蘭\桌面\ CalendarNew-集結桌面/../ CalendarNew // myLabel.moc:70:此處首先定義

調試/ moc_myLabel.o:C:\ Documents和Settings \普拉巴卡蘭\桌面\ CalendarNew-集結桌面/調試/ moc_myLabel.cpp:87:的`MyLabel :: file_Selected(QString的)多個定義」

調試/ myLabel.o:C:\ Documents and Settings \ pr在這裏首先定義了

debug/moc_myLabel.o:moc_myLabel.cpp :(.data + 0x0):多重定義` MyLabel :: staticMetaObject」

調試/ myLabel.o:myLabel.cpp :(數據+爲0x0):此處首先定義

collect2:LD返回1退出狀態

的mingw32-使[1] :* [debug \ CalendarNew.exe]錯誤1

的mingw32-MAK​​E:* [調試]錯誤2

的方法 「d:/TempInstallationFolder/Qt/Dynamic/mingw/bin/mingw32-make.exe」 退出,代碼%2。 錯誤,而建設項目CalendarNew(目標:桌面) 當執行生成步驟「請」

有人請我熟知了這個問題。

回答

2

嘗試刪除線include "MyLabel.moc"形式你的源文件。你不需要把它列入到cpp文件。

+0

哇!你真該死。但是,這部分很奇怪。因爲沒有moc文件,它如何編譯基於Q_Object的類。 – prabhakaran 2011-05-08 07:34:50

+0

@prabhakaran:你必須編譯並與此'moc'文件鏈接,你不需要把它列入。 QT提供參考(http://doc.qt.nokia.com/latest/moc.html)使用MOC編譯器就相當不錯[手動]。 – beduin 2011-05-08 09:07:51