I have a small class that is not working properly, and I can't get what is wrong with it. The compiler gives the message:
main.cpp: error: undefined reference to 'CDetails::CDetails()'
This is the snapshot from the code:
//main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>
#include <QDebug>
class CDetails : public QObject
{ Q_OBJECT
public:
CDetails() {}
~CDetails(void) {}
public slots:
void cppSlot(const QString &msg)
{ qDebug() << "Called the C++ slot with message:" << msg;
}
};
int main(int argc, char *argv[])
{ QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/testqml/main.qml"));
viewer.showExpanded();
CDetails *test = new CDetails();
QObject::connect((QObject*)viewer.rootObject(),
SIGNAL(qmlSignal(QString)),test,
SLOT(cppSlot(QString)));
return app.exec();
}
And in main.qml:
import QtQuick 2.0
Rectangle {
id: guide
width: 360
height: 360
signal qmlSignal(string msg)
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
property double scaleFactor: 1.0
property string iconUrl: "image.png"
MouseArea {
anchors.fill: parent
onClicked: {
guide.qmlSignal("Hello from QML")
}
}
}
Update: Thanks for the suggestion on constructor. Now the error is:
error: undefined reference to 'vtable for CDetails'
What is missed here? All suggestions are welcome.
error: undefined reference to 'vtable for CDetails'
What is missed here? All suggestions are welcome.
Seems you are missing the moc include before the main function.
#include <QtGui/QGuiApplication>
#include <QQmlContext>
#include <QDebug>
class CDetails : public QObject
{ Q_OBJECT
public:
CDetails() {}
~CDetails(void) {}
public slots:
void cppSlot(const QString &msg)
{ qDebug() << "Called the C++ slot with message:" << msg;
}
};
#include "main.moc"
int main(int argc, char *argv[])
{ QGuiApplication app(argc, argv);
QQuickView view;
viewer.setMainQmlFile(QStringLiteral("qml/testqml/main.qml"));
viewer.showExpanded();
CDetails *test = new CDetails();
QObject::connect((QObject*)viewer.rootObject(),
SIGNAL(qmlSignal(QString)),test,
SLOT(cppSlot(QString)));
return app.exec();
}
...
TEMPLATE = app
TARGET = main
QT += quick
SOURCES += main.cpp
...
Note that, you will also need to add your custom lines that were there before, like dealing with the application viewer, et al.
Alternatively, you could also decouple the class and the main.cpp which means you would put the declaration of the class into a separate header, and then the defintition into a separate source file.
The main.cpp would include the freshly established header, and you would need to make sure that the new header and source file are added to the HEADERS
an SOURCES
variables in the qmake project file, respectively to get through the moc processing.
You're missing implementations of your constructor and destructor. Quick fix:
class CDetails : public QObject
{ Q_OBJECT
public:
CDetails() {}
~CDetails(void) {}
...
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With