I took an example program from Qt for qquickview. I am getting an error(undefined reference to 'vtable for ApplicationData', collect2 ld returned 1 exit status) when I add the class in main.cpp. Here is the code
#include<QQuickView>
#include<QGuiApplication>
#include<QObject>
#include<QQmlContext>
#include<QDateTime>
//#include"applicationdata.h"
class ApplicationData
: public QObject{
Q_OBJECT
public:
Q_INVOKABLE
QDateTime getCurrentDateTime
() const { }
};
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QQuickView view;
ApplicationData data;
view.rootContext()->setContextProperty("applicationData", &data);
view.
setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
return app.exec();
}
#include<QQuickView>
#include<QGuiApplication>
#include<QObject>
#include<QQmlContext>
#include<QDateTime>
//#include"applicationdata.h"
class ApplicationData : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE QDateTime getCurrentDateTime() const {
return QDateTime::currentDateTime();
}
};
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QQuickView view;
ApplicationData data;
view.rootContext()->setContextProperty("applicationData", &data);
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
I took the class and placed in applicationdata.h. I didn't get error and the output was perfect. what is the reason I can't understand.
Bookmarks