Can anyone explain this error to me? It seems as though it is an error occurring with moc:
Undefined symbols:
make: Leaving directory `/Users/Dylan/Documents/programming/qt/Clock-build-desktop'
"ClockDelegate::ClockDelegate(QObject*)", referenced from:
AnalogClockDelegate::AnalogClockDelegate(QObject*)in AnalogClockDelegate.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [Clock.app/Contents/MacOS/Clock] Error 1
The process "/usr/bin/make" exited with code 2.
Error while building project Clock (target: Desktop)
When executing build step 'Make'
ClockDelegate:
#ifndef CLOCKDELEGATE_H
#define CLOCKDELEGATE_H
#include <QObject>
class QTime;
class QWidget;
class ClockDelegate : public QObject
{
Q_OBJECT
public:
explicit ClockDelegate(QObject *parent);
virtual void paintClock(QWidget *, QTime *) = 0;
};
#endif // CLOCKDELEGATE_H
AnalogClockDelegate:
#ifndef ANALOGCLOCKDELEGATE_H
#define ANALOGCLOCKDELEGATE_H
#include <QColor>
#include <QPoint>
#include "ClockDelegate.h"
class QWidget;
class AnalogClockDelegate : public ClockDelegate
{
Q_OBJECT
public:
explicit AnalogClockDelegate(QObject *parent);
void paintClock(QWidget *, QTime *);
private:
void setupClockHands();
void drawClockSurface(QWidget *clockView, QTime *);
void drawHourComponent(QWidget *clockView);
void drawMinuteComponent(QWidget *clockView, QTime *);
void drawSecondComponent(QWidget *clockView, QTime *);
QPoint m_center;
QPoint m_hourHand[3];
QPoint m_minuteHand[3];
QPoint m_secondHand[2];
QColor m_hourColor;
QColor m_minuteColor;
QColor m_secondColor;
QColor m_clockFaceColor;
};
#endif // ANALOGCLOCKDELEGATE_H
I think you're missing the "public" keyword, assuming ClockDelegate is a QObject. Otherwise you're not derived from a QObject so you cannot use Q_OBJECT.
class AnalogClockDelegate : public ClockDelegate
Related
I'm new in Qt and I'm trying to create a custom QSlider class (it inherits from QSlider).
This works perfectly but when I try to create new signals and slots, it doesn'ts work. Actually, I think it's the Q_OBJECT macro which doesn't work... Indeed, I get this message from the compiler :
erreur : 1 duplicate symbol for architecture x86_64
erreur : linker command failed with exit code 1 (use -v to see invocation)
If I remove Q_OBJECT it doesn't work either as the compilers tells me :
erreur : Error: Class declaration lacks Q_OBJECT macro.
Finally, if I make my class inherit from QWidget, all is working even the Q_OBJECT macro...
Here is my .h code :
#include <QWidget>
#include <QSlider>
class mySlider : public QSlider
{
Q_OBJECT
public:
explicit mySlider();
signals:
void test();
public slots:
void nTest();
};
Here is my .cpp code (there isn't much things, I'm only trying to make this simple code work for now) :
#include "myslider.h"
mySlider::mySlider()
{
}
void mySlider::test(){
}
void mySlider::nTest() {
}
I want to call a function when a button click. the implementation of the button is in the abstract class. but when I compile I'm getting this error.
This is my .h file of the base class
#ifndef HOME_H
#define HOME_H
#include<QGraphicsScene>
#include <QGraphicsScene>
#include<QPushButton>
class home
{
Q_OBJECT
public:
home();
virtual void set_home_background()=0 ;
QGraphicsScene *scene3;
QPushButton *button3;
private slots:
virtual void startgame1();
};
#endif // HOME_H
This is the base class
#include "home.h"
#include<QGraphicsScene>
#include<QGraphicsProxyWidget>
#include "QMessageBox"
home::home()
{
}
void home::set_home_background()
{
button3 = new QPushButton;
QObject::connect(button3,SIGNAL(clicked()),this,SLOT(startgame1()));
QGraphicsProxyWidget *proxy = this->scene3->addWidget(button3);
button3->setAutoFillBackground(true);
button3->setIcon(QIcon(":/Images/ng.png"));
button3->setIconSize(QSize(131,41));
proxy->setPos(130,430);
scene3->addItem(proxy);
}
void home::startgame1()
{
QMessageBox q;
q.setText("");
q.exec();
}
I'm getting this error
C:\Users\User\Documents\breakout_final\home.cpp:16: error: no matching
function for call to 'QObject::connect(QPushButton*&, const char*,
home*, const char*)'
QObject::connect(button3,SIGNAL(clicked()),this,SLOT(startgame1()));
^
You have an error in your code: in order to use Qt signals and slots, you should inherit your class from QObject, Q_OBJECT declaration itself is not enough:
#include <QObject>
class home : public QObject
{
Q_OBJECT
public:
home();
virtual void set_home_background()=0 ;
QGraphicsScene *scene3;
QPushButton *button3;
private slots:
virtual void startgame1();
};
I'm getting the undefined reference to vtable for CustomProgressBar' error when trying to launch following code:
customprogressbar.h
#ifndef CUSTOMPROGRESSBAR_H
#define CUSTOMPROGRESSBAR_H
#include <QProgressBar>
#include "task.h"
class CustomProgressBar : public QProgressBar
{
Q_OBJECT
public:
CustomProgressBar(DayTask, QWidget* parent = 0);
protected:
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
private:
DayTask task;
};
#endif // CUSTOMPROGRESSBAR_H
customprogressbar.cpp
#include "customprogressbar.h"
#include <QPainter>
CustomProgressBar::CustomProgressBar(DayTask task, QWidget* parent) :
task{task},
QProgressBar(parent)
{
}
//paintevent
What could cause the problem?
Maybe moc (meta object compiler) is not being run for your header?
Anyway, it's duplicate for this question
I know the question have been asked tons of times but I can't find the solution here nor in google.
Here's my header file
#ifndef MAINCONTROLLER_H
#define MAINCONTROLLER_H
#include <QSettings>
#include <QDebug>
#include <QDir>
#include <QObject>
#include "PhTools/PhString.h"
#include "PhStrip/PhStripDoc.h"
class MainController : public QObject
{
Q_OBJECT
public:
explicit MainController(QObject *parent = 0);
void loadSettings();
PhString getLastFile();
void setLastFile(PhString fileName);
bool openDoc(PhString fileName);
signals:
void docChanged();
private:
QSettings * _settings;
PhStripDoc * _doc;
};
#endif // MAINCONTROLLER_H
And my CPP file :
#include "MainController.h"
MainController::MainController(QObject *parent) :
QObject(parent)
{
_doc = new PhStripDoc();
loadSettings();
}
void MainController::loadSettings()
{
_settings = new QSettings(QDir::homePath() + "/Library/Preferences/com.me.me.plist", QSettings::NativeFormat);
getLastFile();
}
PhString MainController::getLastFile()
{
qDebug() << "lastfile :" << _settings->value("last_file", "").toString();
return _settings->value("last_file").toString();
}
bool MainController::openDoc(PhString fileName)
{
bool succeed = _doc->openDX(fileName);
if (succeed)
emit docChanged();
return succeed;
}
void MainController::setLastFile(PhString filename)
{
_settings->setValue("last_file", filename);
}
And here's the error log :
Undefined symbols for architecture x86_64:
"MainController::docChanged()", referenced from:
MainController::openDoc(QString) in MainController.o
"vtable for MainController", referenced from:
MainController::MainController(QObject*) in MainController.o
MainController::MainController(QObject*) in MainController.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It might come from signals but I'm not sure...
When the Q_OBJECT macro is added after the code has already been compiled, qmake has to be run again. This will add the creation and compilation of MainController.moc to your Makefile thus preventing the linker error.
You need to include this line at the end of your source file:
#include "MainController.moc"
Alternatively, you can also handle this with your buildsystem, but that is probably the former is easier.
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.