从JavaScript传递到Qt / C ++对象的字符串 [英] String passing from JavaScript to Qt/C++ object

查看:354
本文介绍了从JavaScript传递到Qt / C ++对象的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows上遇到QtWebKit桥问题,我用完了
选项。我无法访问确切的代码,但它的类似如下。



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); //显示url正确
myObject.data = url; //将网址分配给C ++对象myObject
}

警告框正确显示字符串值,例如10.10.0.1。
setData的参数给我的字符串1。如果我然后查看然后在该地址的内存,我看到内存中的完整的URL(格式化为UTF-32(每个字符4个字节)),但无论我尝试(toStdString,toAscii,utf16 - 只是为了获得敏感的数据)似乎不能获得/使用整个字符串。



I event认为可能调试器正在对我进行欺骗,所以我将数据传递给实际需要此数据的方法(也使用QString)



即使我使MyObject :: setData Q_INVOKABLE和调用setData直接获得相同的
行为:

  myObject.setData(url); //分配url到C ++对象myObject 

如果我将数据作为文字传递, ,如

  myObject.setData('10 .10.0.1'); 

  myObject.data = '10 .10.0.1'; 

我不明白为什么传递一个文字作品,但是一个变量不是,'url'一个字符串类型。

解决方案

我有一部分。字符串传递按预期工作。但是,C ++对象嵌入在网页上并包装ActiveX(ActiveQt)对象。
为此,我看到一些页面,你创建一个继承自QWebPage的类。你有一个createPlugins方法,要求QUiLoader为你构建小部件(或者使用Qt Metatype系统)。和我的C ++对象注册。
当我在WebView上使用这个自定义WebPage(使用setPage)时,字符串被错误地传递。当我禁用调用setPage,字符串正确传递。



因此字符串传递工作(在某些情况下)。



我为此创建了一个新问题: Qt Webkit桥梁ActiveQt字符串


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

这篇关于从JavaScript传递到Qt / C ++对象的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆