Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. undefined reference to name::staticMetaObject when registering with qmlRegisterType
QtWS25 Last Chance

undefined reference to name::staticMetaObject when registering with qmlRegisterType

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmlqmlregistertype
10 Posts 4 Posters 9.0k 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.
  • T Offline
    T Offline
    TTIO
    wrote on last edited by TTIO
    #1

    So I'm not really sure what's going on here, because this worked just fine until I rejigged the directory structure to better cope with unit tests.

    Anyway, the situation is:

    • I have a non-static class that deals with communication with a particular peripheral.
    • Said class is used on a single QML page (though this might change in future), and so I attach it with qmlRegisterType based on this recommendation in the docs.
    • For some reason, my main function fails upon trying to register this type, with error: undefined reference to 'name::staticMetaObject' (where name is the class mentioned earlier)

    Below is the code for main.cpp, if anyone has any suggestions it would be greatly appreciated.

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QDebug>
    #include "../peripherals/name.h"
    #include "../common/dbaccess.h"
    
    int main(int argc, char *argv[])
    {
        qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        qDebug() << "Set up main application window.";
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty()) {
            return -1;
        }
    
        qmlRegisterType<name>();
    
        return app.exec();
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2
      • make sure Q_OBJECT macro is used in your name class, and it is present in private part of class definition
      • delete your build directory, run qmake, build again
      • unrelated but may help later: register the type before you instantiate the QML engine

      (Z(:^

      1 Reply Last reply
      2
      • T Offline
        T Offline
        TTIO
        wrote on last edited by
        #3

        I've got Q_OBJECT right at the top of the class definition, like this:

        class name : public QObject
        {
            Q_OBJECT
        private:
            QSerialPort* signalPort;
            ...
        

        Is that right?
        I've deleted the build directory a few times and it doesn't seem to have helped... But I'll add the type before, thanks.

        sierdzioS 1 Reply Last reply
        0
        • T TTIO

          I've got Q_OBJECT right at the top of the class definition, like this:

          class name : public QObject
          {
              Q_OBJECT
          private:
              QSerialPort* signalPort;
              ...
          

          Is that right?
          I've deleted the build directory a few times and it doesn't seem to have helped... But I'll add the type before, thanks.

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @TTIO said in undefined reference to name::staticMetaObject when registering with qmlRegisterType:

          Is that right?

          Yep that is correct.

          I've deleted the build directory a few times and it doesn't seem to have helped... But I'll add the type before, thanks.

          Hm, pretty sure I've encountered a similar error before but don't remember how I solved it. Other things to try out:

          • add Q_DECLARE_METATYPE for your type in your name.h. In theory it's not needed, in practice it sometimes helps in various QML-related issues
          • try renaming the class to Name (starting with capital letter), or give it a capital letter name in qmlRegisterType<name>("Name") - QML engine expects components to start with capital letter
          • are you using cmake by any chance? If so, remember to run MOC or enable AUTOMOC, so that the meta type information is generated for your name class. With qmake, no extra steps are necessary

          (Z(:^

          1 Reply Last reply
          1
          • T Offline
            T Offline
            TTIO
            wrote on last edited by TTIO
            #5

            Okay, so I put it in caps everywhere, deleted the build directory again, and added this to the end of the header:

            };
            
            Q_DECLARE_METATYPE(Name);
            
            #endif // NAME_H
            

            I'm using qmake, so I skipped the last bit :p

            It's now saying that I'm using a deleted function Name::Name(const Name&). I've defined the constructor and destructor, so I'm not sure why it's unhappy :/

            The full header for Name is:

            #ifndef NAME_H
            #define NAME_H
            
            #include <QObject>
            #include <QMetaType>
            #include "../common/rs485.h"
            #include "../common/dbaccess.h"
            
            class Name : public QObject
            {
                Q_OBJECT
            private:
                QSerialPort* signalPort;
                dbAccess* db;
            public:
                explicit Name(QObject *parent = nullptr);
                ~Name();
            
            signals:
            
            public slots:
                void getMessage(QString key);
                void sendMessage(QString message);
            };
            
            Q_DECLARE_METATYPE(Name);
            
            #endif // NAME_H
            

            (Thank you for all your help so far, by the way!)

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              It's now saying that I'm using a deleted function Name::Name(const Name&)

              This is the copy constructor, it is removed from all QObject classes (Q_DISABLE_COPY()). Where is that error raised? Do you try to do something like:

              Name name1, name2;
              name1 = name2;
              

              anywhere?

              (Z(:^

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                If name derives from QObject there is no need to register it, QML can already manage it

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  TTIO
                  wrote on last edited by TTIO
                  #8

                  No, I've never tried to copy it. It's an object that gets created on a certain app screen and then destroyed when that screen is closed. In fact, I've never even created an instance of Name in C++ code, only ever in QML. The only references to it outside of the class files are in main.cpp and a QML file (plus the pro file, of course).

                  @VRonin said in undefined reference to name::staticMetaObject when registering with qmlRegisterType:

                  If name derives from QObject there is no need to register it, QML can already manage it

                  Yeah, it didn't help. It was just on the off-chance anyway :)

                  VRoninV W 2 Replies Last reply
                  0
                  • T TTIO

                    No, I've never tried to copy it. It's an object that gets created on a certain app screen and then destroyed when that screen is closed. In fact, I've never even created an instance of Name in C++ code, only ever in QML. The only references to it outside of the class files are in main.cpp and a QML file (plus the pro file, of course).

                    @VRonin said in undefined reference to name::staticMetaObject when registering with qmlRegisterType:

                    If name derives from QObject there is no need to register it, QML can already manage it

                    Yeah, it didn't help. It was just on the off-chance anyway :)

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @TTIO said in undefined reference to name::staticMetaObject when registering with qmlRegisterType:

                    Yeah, it didn't help

                    The only reason I can think of why that wouldn't help is that moc is not running on your header file. Try removing it from the project and add it back again

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0
                    • T TTIO

                      No, I've never tried to copy it. It's an object that gets created on a certain app screen and then destroyed when that screen is closed. In fact, I've never even created an instance of Name in C++ code, only ever in QML. The only references to it outside of the class files are in main.cpp and a QML file (plus the pro file, of course).

                      @VRonin said in undefined reference to name::staticMetaObject when registering with qmlRegisterType:

                      If name derives from QObject there is no need to register it, QML can already manage it

                      Yeah, it didn't help. It was just on the off-chance anyway :)

                      W Offline
                      W Offline
                      WatermelonJesus
                      wrote on last edited by
                      #10

                      @TTIO Did you ever fix this? I am having the exact same problem and am thoroughly stumped.

                      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