undefined reference to name::staticMetaObject when registering with qmlRegisterType
-
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(); }
-
- make sure Q_OBJECT macro is used in your
name
class, and it is present inprivate
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
- make sure Q_OBJECT macro is used in your
-
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. -
@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
-
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!)
-
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?
-
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 fromQObject
there is no need to register it, QML can already manage itYeah, it didn't help. It was just on the off-chance anyway :)
-
@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