So I have been climbing the learning curve, created a very slick user interface, putting my GIMP skills to the test, while putting it together very quickly using QtQuick. The next step was to link some C++ functions to the interface. Couldn't find much information, that was correct, that could help me link a C++ application to a QML file.
For a long time I was getting an error in Qt creator for an Undefined reference to vtable, what this means? I am unsure, I am know it is an error linked to the macro Q_OBJECT and the linker, I found that if I include the file main.moc after I have declared the classes then it ran without issue. Check out what Qt have to say about it here.
Using a combination of a few resources I'm posting my findings to hopefully help other Qt newbies in getting up and running quickly.
main.cpp (remember the headers!)
class MyClass : public QObjectExtract from main.qml
{
Q_OBJECT
public slots:
void cppMethod(const QString &msg){
qDebug() << "Called the C++ slot with" << msg;
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView view;
MyClass myClass;
view.rootContext()->setContextProperty("myObject",&myClass);
view.setSource(QUrl::fromLocalFile("qml/linkTest/main.qml"));
view.show();
return app.exec();
}
MouseArea {
id: mouse_area
x: 4
y: 9
width: 442
height: 91
smooth: true
onClicked: {
menuView.currentIndex = index+5
myObject.cppMethod("Hello from QML")
}
}
That code looks... horrible!
ReplyDeleteIndeed it is, this is what I am tasked with mastering.
ReplyDelete