gpt4 book ai didi

c++ - Qt - moc 文件相关错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:58:39 25 4
gpt4 key购买 nike

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

这是头文件。

#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: * [调试] 错误 2

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

任何人都请帮我解决这个问题。

最佳答案

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

关于c++ - Qt - moc 文件相关错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5926038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com