Qt Webkit桥ActiveQt字符串 [英] Qt Webkit bridge ActiveQt string

查看:102
本文介绍了Qt Webkit桥ActiveQt字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另请参阅部分问题:从JavaScript传递到Qt/C ++对象

我在WebView上使用此自定义WebPage(使用setPage),字符串输入错误.当我禁用呼叫setPage时,字符串正确传递.

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.

我有一个C ++对象(继承自QWidget),该对象在网页上显示ActiveX(使用ActiveQtQWebView). ActiveX对象显示正常.我已经注册了自定义类型,并使用Qt MetaType系统实例化了HTML对象.

I have a C++ object (that inherits from QWidget) that displays an activeX on a webpage (using ActiveQt and QWebView). The ActiveX object displays just fine. I've registered the custom type and used the Qt MetaType system to instantiate the HTML object.

如果我想更新该C ++对象(不是ActiveX)的属性并从JavaScript调用,则该字符串会翻译错误.如果传入一些字符串,则在我的C ++方法中只能得到1个字符的字符串.查看内存告诉我整个字符串都存在(格式为UTF-32).

If I want to update a property of that C++ object (not the ActiveX) and call from JavaScript, the string gets a wrong translation or so. If I pass in some string, I only get a string of 1 character on my C++ method. Viewing the memory tells me that the whole string is present (formattted as UTF-32).

如果我现在不调用为我加载插件的自定义WebPage(在QWebView上使用setPage),则无法获得ActiveX(这是预期的),并且字符串正确传递

If I now don't call the custom WebPage that loads the plugin for me (using setPage on QWebView), I don't get the ActiveX - which is expected - and the strings are passed correctly.

我不太明白为什么这行不通.即使调用了setPage,我也是通过JavaScript/QtWebKit桥与C ++对象通信,而不是与ActiveX组件通信.

I don't quite understand why this doesn't work. Even with setPage called, I'm talking to the C++ object via JavaScript/QtWebKit bridge, not to the ActiveX component.

我在Windows 7(64位)上,使用的是Qt 4.7.4.

I'm on Windows 7, 64 bit, using Qt 4.7.4.

某些代码(C ++):

Some code (C++):

class Device : public QWidget
{
    Q_OBJECT

public:
    explicit Device(QWidget* parent = 0);
    Device(const Device& other);
    Device& operator=(const Device& other);
    ~Device();

    QString name() const;
    void setName(QString);
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
signals:
    void nameChanged(QString);    
private:
    QAxWidget* ax;
};
Q_DECLARE_METATYPE(Device);

Device::Device(QWidget* parent/* = 0*/)
    : QWidget(parent)
{
    ax = new QAxWidget(this);
    ax->setControl("{...}");
}

QString Device::name() const
{
    return _name;
}

void Device::setName(QString name)
{
    if (_name != name) {
        _name = name;
        emit nameChanged(name);
    }
}

// Needed for ActiveQt objects to work
class MapPage : public QWebPage
{
    Q_OBJECT
protected:
    QObject* createPlugin(const QString& classId, const QUrl& url, const QStringList& paramNames, const QStringList& paramValues);

public:
    MapPage(QObject* parent = 0);
};

// ============================================================================
// Needed for ActiveQt objects to work
MapPage::MapPage(QObject* parent/* = 0*/)
    : QWebPage(parent)
{
    qRegisterMetaType<Device>();
    settings()->setAttribute(QWebSettings::PluginsEnabled, true);
}

QObject* MapPage::createPlugin(const QString& classId, const QUrl& url, const QStringList& paramNames, const QStringList& paramValues)
{
    // Create widget using QUiLoader, which means widgets don't need to be registered with meta object system (only for gui objects!).
    QUiLoader loader;

    QObject* result = 0;//loader.createWidget(classId, view());

    // If loading failed, use the Qt Metatype system; expect objects derived from QObject
    if (!result) {
        //result = static_cast<QWidget*>(QMetaType::construct(QMetaType::type(classId.toLatin1())));
        int id = QMetaType::type("Device");
        void* classPtr = QMetaType::construct(id);
        result = static_cast<QWidget*>(classPtr);
    }

    return result;
}

然后在我的窗口

qRegisterMetaType<Device>();
mapPage = new MapPage(this);
ui.webView->setPage(mapPage);   // Needed for ActiveQt objects to work - makes strings broken
...
ui.webView->setHtml(src);

推荐答案

将其范围缩小到可能与JavaScript/HTML有关的问题.

Narrowed it down to an issue probably related to JavaScript/HTML.

//device.name = 'test'; // works 
var str = 'test'; 
previewCameo.view = str; // works 

但是当我通过HTML拖放事件(使用e.dataTransfer)获得名称时,它会失败

but when I get the name via a HTML drag-n-drop event (using e.dataTransfer), it fails

var name = e.dataTransfer.getData('name'); 
alert(name); // shows correct name 
device.name = name; // fails <------ 
device.name = e.dataTransfer.getData('name'); // fails <------

因此由于某种原因,dataTransfer数据的检索失败.为什么? 在Linux上,我确实得到了一个字符串,但其格式类似于t [] e [] s [] t [].似乎是编码问题.

So retrieval of the dataTransfer data fails for some reason. Why? On Linux I do get a string back, but its formatted like t[]e[]s[]t[]. Seems like an encoding issue.

这篇关于Qt Webkit桥ActiveQt字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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