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. [SOLVED] (Qt 5) Qt Quick 2 application - will not link when c++ class instance created - cpp is not being built into an .obj
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] (Qt 5) Qt Quick 2 application - will not link when c++ class instance created - cpp is not being built into an .obj

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 3.9k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Dolphin
    wrote on last edited by
    #1

    Just a test project with one class (nothing in it) and one qml file and a main.cpp - my purpose is to learn how to take a c++ variable value and display it via qml.

    Class just has a constructor, nothing else. The project compiles and links until main.cpp creates an instance of my class at which point it tries to find the class.obj file and it is not there. Main.cpp and class.cpp are in the same directory. Shadow build has been disabled. I have tried clean and rebuild, I have even tried deleting the debug output directory manually. Class.h/cpp are in the project file.

    I have created this project twice from scratch and got the same thing:

    • create the project, it runs and displays the qml
    • add a new class to project, still runs
    • add one line into main.cpp creating an instance of class -> link error (cannot find the constructor - which is not surprising if the obj is not there)

    Any ideas at all?

    main.cpp
    @
    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QQuickView *viewer = new QQuickView();
    
    CGuideMenu *cMenu = new CGuideMenu(viewer);
    viewer->setSource(QUrl::fromLocalFile("main.qml"));
    viewer->show();
    
    
    return app.exec();
    

    }
    @

    class header
    @
    class CGuideMenu
    {
    public:
    CGuideMenu(QObject *parent);
    };
    @

    class cpp
    @
    CGuideMenu::CGuideMenu(QObject *parent)
    {
    }
    @

    .pro
    @

    Add more folders to ship with the application, here

    folder_01.source = qml/GuideNext
    folder_01.target = qml
    DEPLOYMENTFOLDERS = folder_01

    Additional import path used to resolve QML modules in Creator's code model

    QML_IMPORT_PATH =

    If your application uses the Qt Mobility libraries, uncomment the following

    lines and add the respective components to the MOBILITY variable.

    CONFIG += mobility

    MOBILITY +=

    The .cpp file which was generated for your project. Feel free to hack it.

    SOURCES += main.cpp
    guidemenu.cpp

    Please do not modify the following two lines. Required for deployment.

    include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
    qtcAddDeployment()

    HEADERS +=
    guidemenu.h
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      -Can you post (or link to) your code and your .pro?- Thanks

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

        What OS and compiler are you using? If you manually run qmake, does it give you any errors?

        It works ok for me, making some assumptions:

        main.cpp has:
        @
        #include <QGuiApplication>
        #include <QQuickView>

        #include "guidemenu.h"
        @

        guidemenu.cpp has:
        @
        #include "guidemenu.h"
        @
        and guidemenu.h has the normal #ifndef/#define/#endif guards.

        and your .pro file has
        @
        QT += quick
        @

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dolphin
          wrote on last edited by
          #4

          I am using Qt 5 on Windows 7.

          QT += quick is that in the .pro?? I have posted the whole .pro file and obv it is not there :-)

          I will try that first when I am work tomorrow.

          Thank you :-)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dolphin
            wrote on last edited by
            #5

            Yep - that did it. After nearly 2 decades of c code I really did not think I had made an error in the actual classes (and I got someone to check!). It is these setting things that trip you up - I will learn!

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dolphin
              wrote on last edited by
              #6

              One more quick question. CGuideMenu is derived from QObject and has Q_OBJECT in header but I still get the following error

              main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall CGuideMenu::metaObject(void)const " (?metaObject@CGuideMenu@@UBEPBUQMetaObject@@XZ)

              for main.obj

              main.cpp
              @
              #include <QtGui/QGuiApplication>
              #include "qtquick2applicationviewer.h"

              #include <QObject>
              #include <QUrl>
              #include <QQuickView>
              #include <QQuickItem>
              #include <QQmlContext>
              #include <QVariant>
              #include "guidemenu.h"

              int main(int argc, char *argv[])
              {
              QGuiApplication app(argc, argv);

              qmlRegisterType<CGuideMenu>("CGuideMenu", 1, 0, "CGuideMenu");
              
              QQuickView viewer;
              
              CGuideMenu *cMenu = new CGuideMenu(&viewer);
              // With that below you can call myMenu.getDisplayString() in QML
              

              //?? neither lines compile????
              viewer.rootContext()->setContextProperty("myMenu", QVariant::fromValue(cMenu));
              viewer.rootContext()->setContextProperty("myMenu", cMenu);

              viewer.setSource(QUrl::fromLocalFile&#40;"main.qml"&#41;&#41;;
              viewer.show(&#41;;
              

              // QQuickItem *window = viewer.rootObject();
              // window->setProperty("heading", "BOOH");

              return app.exec&#40;&#41;;
              

              }
              @

              guidemenu.h
              @
              #ifndef GUIDEMENU_H
              #define GUIDEMENU_H
              #include <QObject>

              class CGuideMenu : public QObject
              {
              Q_OBJECT

              public:
              CGuideMenu(QObject *parent = 0);
              };

              #endif // GUIDEMENU_H
              @

              cpp
              @
              #include "guidemenu.h"

              CGuideMenu::CGuideMenu(QObject *parent) : QObject(parent)
              {
              parent = parent;
              }
              @

              pro
              @

              Add more folders to ship with the application, here

              folder_01.source = qml/GuideNext
              folder_01.target = qml
              DEPLOYMENTFOLDERS = folder_01

              QT += core qml quick

              Additional import path used to resolve QML modules in Creator's code model

              QML_IMPORT_PATH =

              If your application uses the Qt Mobility libraries, uncomment the following

              lines and add the respective components to the MOBILITY variable.

              CONFIG += mobility

              MOBILITY +=

              The .cpp file which was generated for your project. Feel free to hack it.

              SOURCES += main.cpp guidemenu.cpp

              Please do not modify the following two lines. Required for deployment.

              include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
              qtcAddDeployment()

              HEADERS += guidemenu.h

              @

              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