Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. I became crazy about LNK2001 error.. in very small qml test...
Qt 6.11 is out! See what's new in the release blog

I became crazy about LNK2001 error.. in very small qml test...

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 818 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    filipdns
    wrote on last edited by filipdns
    #1

    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() }
    }
    

    context.pro

    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 problem

    When I remove
    Q_OBJECT

    no error but nothing on qml page off course...

    Do you have an idea why I got this?...

    Thank you for your help

    DiracsbracketD 1 Reply Last reply
    0
    • F filipdns

      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() }
      }
      

      context.pro

      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 problem

      When I remove
      Q_OBJECT

      no error but nothing on qml page off course...

      Do you have an idea why I got this?...

      Thank you for your help

      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by Diracsbracket
      #2

      @filipdns
      Put the QObject -derived class using the Q_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.

      F 1 Reply Last reply
      5
      • DiracsbracketD Diracsbracket

        @filipdns
        Put the QObject -derived class using the Q_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.

        F Offline
        F Offline
        filipdns
        wrote on last edited by
        #3

        @Diracsbracket Great, thanks a lot!! I'm too stupid to didn't think that way before ;-)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved