I found some qmlRegisterType examples on the Internet, but just can't get it to work. I create a new Qt Quick 2 project and add the following content:
This is my .pro file:
folder_01.source = qml/testlib
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01
QT += core gui widgets
QML_IMPORT_PATH =
HEADERS += main.hpp
SOURCES += main.cpp
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
qtcAddDeployment()
This is my code file (main.cpp):
#include <QtQml>
#include <QtWidgets/QApplication>
#include "qtquick2applicationviewer.h"
class TestClass:public QObject {
Q_OBJECT
public:
TestClass(QObject* parent=0):QObject(parent){}
public slots:
void test() {
qDebug("test!");
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
qmlRegisterType<TestClass>("testlib",1,0,"TestClass");
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/testlib/main.qml"));
viewer.showExpanded();
return app.exec();
}
This is my QML file (main.qml):
import QtQuick 2.0
import testlib 1.0
Rectangle {
TestClass {
id:testobj
}
Component.onCompleted: {
testobj.test();
}
}
But I encounter several binding errors:
- undefined reference to `vtable for TestClass'
- undefined reference to `TestClass :: staticMetaObject '
- undefined reference to `TestClass :: metaObject () const '
- undefined reference to `TestClass :: qt_metacast (char const *) '
- undefined reference to `TestClass :: qt_metacall (QMetaObject :: Call, int, void **) '

Qt 5.2.1, MinGW 4.8 stable. , , ( ). ? Qt 5, Qt 4?