Wednesday, October 19, 2011

Qt: Undefined reference to vtable


For the past few weeks I have been evaluating various tool-kits for developing glossy user interfaces that you would typically see on a smartphone these days. It had been to toss up between Inflexion from Mentor Graphics or Qt from Nokia. Obviously I have fallen on to the side of Qt for the option of using the SDK under LGPL.

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 QObject
{
    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();
}
Extract from main.qml
MouseArea {
      id: mouse_area
      x: 4
      y: 9
      width: 442
      height: 91
      smooth: true

      onClicked: {
          menuView.currentIndex = index+5
          myObject.cppMethod("Hello from QML")
      }
}
 

2 comments:

  1. That code looks... horrible!

    ReplyDelete
  2. Indeed it is, this is what I am tasked with mastering.

    ReplyDelete