0

I am having problems using the QObject. In my code I attempt to add a object to Javascript in Qt5.

#include <QtGui>
#include <QtWebKit>
#include <QApplication>
#include <QWebView>
#include <QWebFrame>
#include <QWebPage>
#include <iostream>
#include <string>

using namespace std;

class nativeObject : public QObject {
    Q_OBJECT
    public:
        string version;
};

int main(int argc, char** argv) {
    nativeObject test;
    test.version = "BETA";

    QApplication app(argc, argv);
    QWebView view;
    QWebFrame *frame = view.page()->mainFrame();
    frame->addToJavaScriptWindowObject("someNameForMyObject", &test);
    view.setUrl(QUrl("http://google.com"));
    view.show();

    return app.exec();
}

Running the code gives the following errors:

main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall nativeObject::metaObject(void)const " (?metaObject@nativeObject@@UBEPBUQMetaObject@@XZ)

main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall nativeObject::qt_metacast(char const *)" (?qt_metacast@nativeObject@@UAEPAXPBD@Z)

main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall nativeObject::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@nativeObject@@UAEHW4Call@QMetaObject@@HPAPAX@Z)

release\webkit.exe:-1: error: LNK1120: 3 unresolved externals

I cannot find any "good" and relevant documentation as I am very new in programming in qt and C++. Have I declared the QObject incorrectly or is there something else I am doing wrong?

Xweque
  • 605
  • 1
  • 9
  • 29

1 Answers1

1

Try this:

frame->addToJavaScriptWindowObject("someNameForMyObject", &test);

It requires QObject* but you set QObject.

void QWebFrame::addToJavaScriptWindowObject(const QString & name, QObject * object, ValueOwnership own = QtOwnership)

& used here to get address of object because it is not a pointer, you can also create nativeObject as pointer:

nativeObject *test = new nativeObject;

In this case

frame->addToJavaScriptWindowObject("someNameForMyObject", test);

will be valid because test is already pointer. Note that for pointers we use -> instead of .

Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • It now return a different error... 3 "unresolved external symbol". The errors in full are too big to post in the comments. The same errors occur using both methods you explained above. – Xweque Nov 10 '14 at 19:48
  • @Xweque Edit your answer and add full new code and full errors – Jablonski Nov 10 '14 at 19:50
  • @Xweque now let me guess, if you do Build - Run Qmake all will be good, errors will disappeae but if you run project after this you will get this errors again. Am I right? – Jablonski Nov 10 '14 at 20:18
  • @Xweque ok, unfortunately I from mobile now, so I can't get full answer. I will be able to do this tomorrow, but now one more guess. Try to write this class in separate h and cpp files and include it to main( don't write this class in main) Tell me results, if all fine then fine:) if no, then tommorow I will try to solve it – Jablonski Nov 10 '14 at 20:33
  • Unfortunately it still return the same errors... Really greatful your helping me out though :) – Xweque Nov 10 '14 at 20:42
  • @Xweque Why am I guessing?:) because i use mingw and creator but you use vs so our errors are different but caused by similar things. So check Please this answer: http://stackoverflow.com/questions/6642823/qt-metaobject-linker-problem i think it is the best explanation – Jablonski Nov 10 '14 at 21:16