Qt - moc file related error - c++

I am coding a project. I started to getting errors after I tried to make the static build. I made some changes, which I can't remember. But, I am sure that if this stub can be cleared, then the main project also can be get cleared.
This is header file.
#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
Here is the source file
#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"
And this is the main file
#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();
}
I created the moc file using qt command prompt and the command
moc myLabel.h -o myLabel.moc
After this I tried to compile the project through Qt-Editor. But I got multi definition error as follows,
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: first defined here
debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:62: multiple definition of `MyLabel::qt_metacast(char const*)'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:62: first defined here
debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:70: multiple definition of `MyLabel::qt_metacall(QMetaObject::Call, int, void**)'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:70: first defined here
debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:87: multiple definition of `MyLabel::file_Selected(QString)'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:87: first defined here
debug/moc_myLabel.o:moc_myLabel.cpp:(.data+0x0): multiple definition of `MyLabel::staticMetaObject'
debug/myLabel.o:myLabel.cpp:(.data+0x0): first defined here
collect2: ld returned 1 exit status
mingw32-make[1]: * [debug\CalendarNew.exe] Error 1
mingw32-make: * [debug] Error 2
The process "D:/TempInstallationFolder/Qt/Dynamic/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project CalendarNew (target: Desktop)
When executing build step 'Make'
Anybody please hep me out of this problem.

Try to remove line include "MyLabel.moc" form you source file. You don't need to include it into cpp file.

Related

qt - undefined reference to `vtable for myObj' in qt console application - signals and slots

I need to capture emited signals from a QProcess for testing purposes.
Since I am using a console application, I resolved to create a class in my main.cpp file called myObj using mainly this example:
#include <QCoreApplication>
#include <QLoggingCategory>
#include <QTextStream>
#include <QProcess>
#include <QString>
#include <QVariant>
#include <QDebug>
#include <QObject>
class myObj : public QObject
{
Q_OBJECT
public:
myObj(QObject *parent = 0);
// virtual ~Communicate();
~myObj();
public slots:
void registerFinished(int signal);
void registerAboutToClose();
void registerChannelReadyRead(int signal);
void registerReadChannelFinished();
void registerReadyRead();
void registerReadyReadStandardOutput();
void registerStarted();
};
myObj::myObj(QObject *parent)
: QObject(parent) <--- LINE 72 Error
{
}
//virtual myObj::~Communicate(){
//}
myObj::~myObj(){ <--- LINE 81 Error
}
void myObj::registerFinished(int signal){
qDebug() << "exit code = " << QString::number(signal);
}
void myObj::registerAboutToClose(){
qDebug() << "aboutToClose";
}
void myObj::registerChannelReadyRead(int signal){
qDebug() << "channelReadyRead = " << QString::number(signal);
}
void myObj::registerReadChannelFinished(){
qDebug() << "readChannelFinished";
}
void myObj::registerReadyRead(){
qDebug() << "exit code";
}
void myObj::registerReadyReadStandardOutput(){
qDebug() << "exit code";
}
void myObj::registerStarted(){
qDebug() << "started";
}
myObj *myO;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
myO = new myObj();
//....
}
Problem:
main.cpp:72: error: undefined reference to `vtable for myObj'
main.cpp:81: error: undefined reference to `vtable for myObj'
I have looked at a number of SO pages e.g here and here and here and various others, yet had not found a solution
I have tried/done:
added the Q_Object Macro
ran qmake
rebuilt
checked the #include
.pro file
QT += core
QT -= gui
CONFIG += c++11
TARGET = serv_app
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
Any suggestions?
You have two options:
#eyllanesc solution works for sure.
add the line #include "main.moc" before or after you main() function.
When you put the class into its own header file, qmake will generate the proper moc file.
But when you put the class into a .cpp file, the moc code is not generated unless you put the line I said before.
Update #1
In the Qt tutorial about writing a Unit Test we can find the following info:
Note that if both the declaration and the implementation of our test
class are in a .cpp file, we also need to include the generated moc
file to make Qt's introspection work.
So this is another example where we need to include the moc file.

How can I fix this error?

I came across this error when I tried to run this example code while reading the book - "C++ GUI programming with Qt".
gotocelldialog.h :
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include <QDialog>
#include "ui_gotocelldialog.h"
class gotocelldialog : public QDialog, public Ui::goToCellDialog
{
Q_OBJECT
public:
explicit gotocelldialog(QWidget *parent = 0);
signals:
public slots:
void on_lineEdit_textChanged();
};
#endif // GOTOCELLDIALOG_H
gotocelldialog.cpp :
#include "gotocelldialog.h"
#include <QtGui>
gotocelldialog::gotocelldialog(QWidget *parent) :
QDialog(parent)
{
setupUi(this);
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
lineEdit->setValidator(new QRegExpValidator(regExp,this));
connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));
connect(cancelButton,SIGNAL(clicked()),this,SLOT(accept()));
}
void gotocelldialog::on_lineEdit_textChanged(){
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
main.cpp:
#include <QApplication>
#include <QDialog>
#include "gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
gotocelldialog* dialog = new gotocelldialog();
dialog->show();
return a.exec();
}
When I run this program , it displays this error message :
/home/ayush/untitled3/gotocelldialog.cpp:6: error: no matching function for call to 'gotocelldialog::setupUi(gotocelldialog*)'
setupUi(this);
^
../untitled3/gotocelldialog.cpp:6:17: note: candidate is:
In file included from ../untitled3/gotocelldialog.h:5:0,
from ../untitled3/gotocelldialog.cpp:1:
./ui_gotocelldialog.h:49:10: note: void Ui_goToCellDialog::setupUi(QMainWindow*)
void setupUi(QMainWindow *goToCellDialog)
^
./ui_gotocelldialog.h:49:10: note: no known conversion for argument 1 from 'gotocelldialog*' to 'QMainWindow*'
Makefile:344: recipe for target 'gotocelldialog.o' failed
make: *** [gotocelldialog.o] Error 1
17:15:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project untitled3 (kit: Desktop)
When executing step 'Make'
What is the reason for this error and how can this be solved ?
Looks like your ui file defines a QMainWindow, but that your code expects a QDialog.
So there's a mismatch, your should probably recreate the ui file from the QDialog template, or change it in an editor.

Qt C++ - Can't add slots

When I add slots to my script it will no longer build.
inkpuppet.obj:-1: error: LNK2005: "private: void __cdecl InkPuppet::on_aboutButton_clicked(void)" (?on_aboutButton_clicked#InkPuppet##AEAAXXZ) already defined in main.obj
and
debug\InkPuppet.exe:-1: error: LNK1169: one or more multiply defined symbols found
Here is the code:
inkpuppet.h - commenting out void on_aboutButton_clicked(); and the function at the end will make it run.
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtCore>
namespace Ui {
class InkPuppet;
}
class InkPuppet : public QWidget
{
Q_OBJECT
public:
explicit InkPuppet(QWidget *parent = 0);
~InkPuppet();
private:
Ui::InkPuppet *ui;
private slots:
void on_aboutButton_clicked();
};
#endif // WIDGET_H
void InkPuppet::on_aboutButton_clicked()
{
}
inkpuppet.cpp
#include "inkpuppet.h"
#include "ui_inkpuppet.h"
InkPuppet::InkPuppet(QWidget *parent) :
QWidget(parent),
ui(new Ui::InkPuppet)
{
ui->setupUi(this);
//connect(ui->lowerFrameBox, SIGNAL(valueChanged(int)), ui->timeSlider, SLOT(setRange(int,int)));
}
InkPuppet::~InkPuppet()
{
delete ui;
}
main.cpp
#include "inkpuppet.h"
#include "aboutdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
InkPuppet w;
w.show();
return a.exec();
}
aboutdialog.h
#ifndef ABOUTDIALOG_H
#define ABOUTDIALOG_H
#include <QDialog>
namespace Ui {
class AboutDialog;
}
class AboutDialog : public QDialog
{
Q_OBJECT
public:
explicit AboutDialog(QWidget *parent = 0);
~AboutDialog();
private:
Ui::AboutDialog *ui;
};
#endif // ABOUTDIALOG_H
aboutdialog.cpp
#include "aboutdialog.h"
#include "ui_aboutdialog.h"
AboutDialog::AboutDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AboutDialog)
{
ui->setupUi(this);
}
AboutDialog::~AboutDialog()
{
delete ui;
}
You define your void InkPuppet::on_aboutButton_clicked() in your inkpuppet.h. And then you include it in inkpuppet.cpp AND in main.cpp -> one or more multiply defined symbols found.
Put
void InkPuppet::on_aboutButton_clicked()
{
}
in your inkpuppet.cpp file.
If the first file you pasted is a whole, there's a problem with the include guards. The definition is after the guard's end.
#endif // WIDGET_H
void InkPuppet::on_aboutButton_clicked()
{
}
Your definition is right after the #endif which means as soon as in the same translation unit, the header is included twice, you'll get this error. And this happens in your code because inkpuppet.h is included in both main.cpp and in inkpuppet.cpp You should put the implementation code for on_aboutButton_clicked() in your inkpuppet.cpp file.

"First defined here" error

I have an error that i can't include my header file in more than one cpp even though i have guard headers.
when removing the include of DatabaseManager from main the ccode builds just fine
here is the header file :
#ifndef DATABASEMANAGER_H
#define DATABASEMANAGER_H
#include <QSqlDatabase>
#include <QSqlQuery>
class DatabaseManager
{
private:
QSqlDatabase PatternLibrary;
QSqlQuery query;
public:
DatabaseManager();
};
#endif
here is the .cpp:
#include "DatabaseManager.h"
#include <QSqlError>
#include <QDebug>
DatabaseManager::DatabaseManager()
{
}
and here is the main :
#include "DatabaseManager.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DatabaseManager x;
MainWindow w;
w.show();
return a.exec();
}
giving these errors :
/Code/DB_RangePattern-build-desktop-Qt_4_8_1_in_PATH_System_Debug/../DB_RangePattern/main.cpp:6: error: first defined here
collect2: ld returned 1 exit status
You've only posted one line of a larger error, but I can hazard a guess at what the problem is. You seem to be unsure of whether your class is DataBaseManager or DatabaseManager (note the change in capital B).
Also, if your header file is with the rest of your source files, make sure you're doing #include "DatabaseManager.h" (not using < and >).
I am pretty sure QSqlDatabase uses/include QSqlError because it has a defined public function
QSqlError lastError () const
and redefinition will come from your including QSqlError

Qt phonon media object error

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <Phonon/MediaSource>
#include <QUrl>
#include <Phonon/MediaObject>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QUrl url("http://www.example.com/music.ogg");
Phonon::MediaObject *wow =
Phonon::createPlayer(Phonon::NoCategory,
Phonon::MediaSource(url));
wow->play();
}
This code won't play the file, and I'm getting this error:
:: error: collect2: ld returned 1 exit status
Can anyone help me get the file to play when I click the button?
Thanks.
I guess that there are one or more functions declared in the header file but their bodies haven't been built yet.
for example:
//headerfile
class MyClass
{
public: MyClass();
private: void function1();
void function2();
};
//source file
MyClass::MyClass(){}
void MyClass::function1(){ /*do something*/ }
//here function2 is missing.
So, please check that all the functions in the whole project have their bodies.
For a basic phonon media player,
#ifndef MYVIDEOPLAYER_H
#define MYVIDEOPLAYER_H
#include <QWidget>
#include <QPushButton>
#include <Phonon/VideoPlayer>
#include <QVBoxLayout>
class MyVideoPlayer : public QWidget
{
Q_OBJECT
public:
explicit MyVideoPlayer(QWidget *parent = 0);
private:
Phonon::VideoPlayer *videoPlayer;
QPushButton *btnButton;
QVBoxLayout layout;
private slots:
void onPlay();
};
#endif // MYVIDEOPLAYER_H
#include "myvideoplayer.h"
MyVideoPlayer::MyVideoPlayer(QWidget *parent) :
QWidget(parent)
{
videoPlayer=new Phonon::VideoPlayer(Phonon::VideoCategory,this);
btnButton=new QPushButton("Play",this);
layout.addWidget(btnButton);
layout.addWidget(videoPlayer);
setLayout(&layout);
connect(btnButton,SIGNAL(clicked()),this,SLOT(onPlay()));
}
void MyVideoPlayer::onPlay()
{
videoPlayer->load(Phonon::MediaSource("movie.mp4"));
videoPlayer->play();
}
As templatetypedef commented, it sounds like a linker error. Ensure that you have added all of the necessary libraries to the .pro file. For example, you need to link against Phonon, so your .pro file must contain
QT += phonon