I’m stuck on a QtWebKit bridge issue on Windows and I ran out of
options. I don’t have access to the exact code used but its something like below.
C++
class MyObject : QWidget {
Q_OBJECT
public:
QString data() const;
void setData(QString);
Q_PROPERTY(QString data READ data WRITE setData)
};
/* ... */
MyObject myObject;
frame->addToJavaScriptWindowObject("myObject", &myObject);
JavaScript
function drop(e) {
var url = e.dataTransfer.getData('url');
//alert(url); // Displays url correctly
myObject.data = url; // Assigns url to C++ object myObject
}
The alert box correctly displays the string value, e.g. 10.10.0.1.
The parameter on setData gives me the string “1”. If I then view then memory at that address, I see the full url in memory (formatted as UTF-32 (4 bytes per character)), but whatever I try (toStdString, toAscii, utf16 – just to get sensible data) I do not seem to be able to get/use the whole string.
I event thought that maybe the debugger is playing a trick on me, so I pass the data to the method that actually needs this data (which also used a QString) it all might work – but sadly no.
Even if I make MyObject::setData Q_INVOKABLE and call setData directly I get the same
behaviour:
myObject.setData(url); // Assigns url to C++ object myObject
If I just pass the data as literal, all DOES work correctly, like
myObject.setData('10.10.0.1');
or
myObject.data = '10.10.0.1';
I do not understand why passing a literal works but a variable not, the ‘url’ variable should be a string type.
I’m partly there. String passing works as expected. However, the C++ object is embedded on a webpage and wraps an ActiveX (ActiveQt) object.
For this, I’ve seen some pages where you create a class that inherits from a QWebPage. There you have a createPlugins method that asks the QUiLoader to construct the widget for you (alternatively you use the Qt Metatype system). And my C++ object is registered.
When I use this custom WebPage on my WebView (using setPage), the strings are passed in wrongly. When I disable calling setPage, the strings passed correctly.
So string passing works (in some conditions).
I’ve created a new issue for this: Qt Webkit bridge ActiveQt string