Reputation: 645
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?
Upvotes: 0
Views: 225
Reputation: 18504
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 .
Upvotes: 1