PDA

View Full Version : Pointer in Q_PROPERTY



pkorzeniewski
19th September 2012, 00:00
Hello,

I have a problem accessing pointer property, defined as Q_PROPERTY, in JavaScript using QWebKit - here is a simple example:



// MyObject.h
class MyObject : public QObject {
Q_OBJECT

public:
Q_PROPERTY (QString* version READ getVersion)

QString* getVersion();

QString* version;

public slots:
void populateJavaScriptWindowObject();
};

// MyObject.cpp
MyObject::MyObject() {
qRegisterMetaType<QString*>("QString*");

version = new QString("1.0");

connect(app->getWebView()->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(populateJavaScriptWindowObject()));
}

QString* MyObject::getVersion() {
return version;
}

void MyObject::populateJavaScriptWindowObject() {
app->getWebView()->page()->mainFrame()->addToJavaScriptWindowObject("my", this);
}


In JavaScript I should get following object:



my: {version: "1.0"}


but instead I get:



my: {version: ""}


I will be grateful for any help, thanks!

Regards,
Piotr

Jonny174
19th September 2012, 04:01
http://stackoverflow.com/questions/6959501/qt-q-property-with-pointer-and-forward-declaration-for-qtscript-access

platon
19th September 2012, 09:54
It's better to expose QString object here because it is a standard QtScript type and will it be automatically converted to JS string.
In this case qRegisterMetaType won't be needed.


QString language();
Q_PROPERTY(QString language READ language SCRIPTABLE true);



QString mLanguage;



QString MediaComponent::language()
{
return mLanguage;
}

wysota
20th September 2012, 10:32
... And it won't create memory leaks.