I became crazy about LNK2001 error.. in very small qml test...
-
hello,
I'm trying to use example from qt doc and I receive an error that I don't understand where is the problem...
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <QDateTime> class ApplicationData : public QObject { Q_OBJECT public: Q_INVOKABLE QDateTime getCurrentDateTime() const { return QDateTime::currentDateTime(); } }; int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; ApplicationData data; engine.rootContext()->setContextProperty("applicationData", &data); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
main.qml
import QtQuick 2.9 import QtQuick.Window 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") Text { text: applicationData.getCurrentDateTime() } }
QT += quick qml CONFIG += c++11 # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp RESOURCES += qml.qrc # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = # Additional import path used to resolve QML modules just for Qt Quick Designer QML_DESIGNER_IMPORT_PATH = # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS +=
I received 3 error:
main.obj:-1: erreur : LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall ApplicationData::metaObject(void)const " (?metaObject@ApplicationData@@UBEPBUQMetaObject@@XZ)
main.obj:-1: erreur : LNK2001: unresolved external symbol "public: virtual void * __thiscall ApplicationData::qt_metacast(char const *)" (?qt_metacast@ApplicationData@@UAEPAXPBD@Z)
main.obj:-1: erreur : LNK2001: unresolved external symbol "public: virtual int __thiscall ApplicationData::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@ApplicationData@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
What I did:
Delete builded folder
qmake
run
same problem...clean project
qmake
run
same problemWhen I remove
Q_OBJECTno error but nothing on qml page off course...
Do you have an idea why I got this?...
Thank you for your help
-
@filipdns
Put theQObject
-derived class using theQ_OBJECT
macro into a header file such that the MOC can find it and process it.
http://doc.qt.io/qt-5/moc.html#ifndef APPLICATIONDATA_H #define APPLICATIONDATA_H #include <QObject> #include <QDateTime> class ApplicationData : public QObject { Q_OBJECT public: Q_INVOKABLE QDateTime getCurrentDateTime() const { return QDateTime::currentDateTime(); } }; #endif // APPLICATIONDATA_H
I agree that the example in
http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html#setting-an-object-as-a-context-property
does not make this clear and is highly confusing. -
@Diracsbracket Great, thanks a lot!! I'm too stupid to didn't think that way before ;-)