如何在 QWebView 中获取 Javascript 来创建 C++ 的新实例 基础课程?

发布于 2024-07-22 04:23:08 字数 596 浏览 16 评论 0原文

我已使用 addToJavaScriptWindowObject 成功将 C++ 对象添加到 QWebFrame, 并且可以从 javascript 调用该对象上的插槽。

但我真正想做的是让其中一个插槽返回一个新对象。 例如,我有一个像这样的槽,它返回一个 QObject 派生类实例:

   MyObject* MyApp::helloWorld()
   {
          //MyObject is dervied from QObject
          return new MyObject();
   }

我可以像这样从 javascript 成功调用此槽

   var foo=myapp.helloWorld();

但 foo 似乎是空的,我无法调用任何槽或访问任何槽 来自 Javascript 的属性。

关于如何实现这一目标有什么想法吗?

I've successfully added an C++ object to a QWebFrame with addToJavaScriptWindowObject,
and can call a slot on that object from javascript.

But what I really want to do is have one of those slots return a new object. For example, I have a slot like this, which returns a QObject derived class instance:

   MyObject* MyApp::helloWorld()
   {
          //MyObject is dervied from QObject
          return new MyObject();
   }

I can call this slot from javascript successfully like this

   var foo=myapp.helloWorld();

But foo appears to be empty, I can't call any slots or access any
properties on it from Javascript.

Any ideas on how I can achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

在风中等你 2024-07-29 04:23:09

QtScript 具有原型的概念 - 它允许您为脚本值创建 C++ 原型。 我们正在研究是否可以将 QtScript 与 JavaScriptCore 桥接 - 这应该导致也可以使用来自 WebKit 的 JavaScript 环境的原型; http://doc.trolltech.com/ 4.5/qtscript.html#making-use-of-prototype-based-inheritance

QtScript has the notion of prototypes - which allows you to create a C++ prototype for a script value. We are investigating wether we can bridge QtScript with JavaScriptCore - which should result in the possibility of using prototypes from WebKit's JavaScript environment as well; http://doc.trolltech.com/4.5/qtscript.html#making-use-of-prototype-based-inheritance

等待我真够勒 2024-07-29 04:23:09

尝试将新对象作为 QObject* 而不是 MyObject* 返回。 如果您只是使用 QtScript,那么您可以调用 qScriptRegisterMetaType 来定义一些用于处理将 MyObject*s 转换为 QScriptValues (或 QVariants)的代码,但似乎没有 QtWebKit 中使用的 JavaScript 引擎的等效代码。

令人烦恼的是,这意味着将内部对象模型暴露给 WebKit 将涉及到使用一组单独的代理函数将对象指针转换为 QObject*s,或者使用某种适配器类来完成相同的操作。

Try returning your new object as a QObject* rather than a MyObject*. If you're just working with QtScript, then you can call qScriptRegisterMetaType to define some code for handling conversion of MyObject*s into QScriptValues (or QVariants), but there doesn't seem to be an equivalent for the JavaScript engine used in QtWebKit.

Annoyingly, this means that exposing your internal object model to WebKit will involve either having a separate set of proxy functions that convert your object pointers to QObject*s, or using adapter classes of some kind to do the same thing.

辞慾 2024-07-29 04:23:09

这个答案是基于保罗的答案,但有点简化。 已针对 Qt 5.3 进行测试和工作。 您需要一个工厂来实例化一个对象,然后返回一个指向该对象的 QObject 指针。 该对象的类必须继承自QObject,以便它在 JavaScript 中正常工作:

QObject * MyApp::createInstance(QString objname) {
    MyClass * obj = new MyClass(this);
    m_mainWindow->webView->page()->mainFrame()->addToJavaScriptWindowObject(objname, obj, QWebFrame::ScriptOwnership);
    return obj;
}

有了这个,您可以从 Javascript 执行以下操作:

var myobject = MyApp.createInstance("obj1");

此时,您也有了 obj1作为全局 JavaScript 命名空间中的 myobject (它们只是指针,因此执行以下两项工作:

myobject.testmethod();
obj1.testmethod();

此时,您可以在 JavaScript 中使用 connect 将 C++ 信号连接到JavaScript 插槽。有关此技术的更多信息,请访问:http://doc.qt.io /qt-5/qtwebkit-bridge.html

This answer is based on Paul's answer, but a bit simplified. Tested and working for Qt 5.3. You need a factory that instantiates an object, and then returns a QObject pointer to this object. The object's class has to inherit from QObject so that it works properly in JavaScript:

QObject * MyApp::createInstance(QString objname) {
    MyClass * obj = new MyClass(this);
    m_mainWindow->webView->page()->mainFrame()->addToJavaScriptWindowObject(objname, obj, QWebFrame::ScriptOwnership);
    return obj;
}

With this, you can do the following from Javascript:

var myobject = MyApp.createInstance("obj1");

At this point, you have obj1 as well as myobject in the global JavaScript namespace (they are just pointers, so doing both of the following works:

myobject.testmethod();
obj1.testmethod();

At this point, you can use connect in JavaScript to connect C++ signals to JavaScript slots. More information on this technique here: http://doc.qt.io/qt-5/qtwebkit-bridge.html

转身泪倾城 2024-07-29 04:23:08

我考虑过的一个相当丑陋的黑客方法是使用 addToJavaScriptWindowObject 将我想要返回的对象放入具有随机名称的窗口对象中,然后让我的插槽返回对象实例的名称:

QString MyApp::helloWorld()
{
     //general a unique name for the js variable
     QString name=getRandomVariableName();

     //here's the object we want to expose to js
     MyObject* pReturn=new MyObject();

     //we make attach our object to the js window object    
     getWebFrame()->addToJavaScriptWindowObject(name, pReturn,
         QScriptEngine::ScriptOwnership);  

     //tell js the name we used
     return name;
}

可以编写 JS 来检查是否返回值是一个字符串,如果是,则从窗口中抓取该对象。:

var foo=myapp.helloWorld();
if (typeof foo == "string")
{
    foo=window[foo];
}

有点难看,但会帮助我,直到出现更好的方法。 未来的 Qt 版本将统一脚本支持,以便它全部基于 WebKit 中的 JavaScriptCore,所以希望这会有所改进!

One rather ugly hack I've considered is to use addToJavaScriptWindowObject to drop the object I want to return into the window object with a random name, then have my slot return the name of the object instance instead:

QString MyApp::helloWorld()
{
     //general a unique name for the js variable
     QString name=getRandomVariableName();

     //here's the object we want to expose to js
     MyObject* pReturn=new MyObject();

     //we make attach our object to the js window object    
     getWebFrame()->addToJavaScriptWindowObject(name, pReturn,
         QScriptEngine::ScriptOwnership);  

     //tell js the name we used
     return name;
}

The JS can be written to check if the return value is a string, and if it is, grab the object from the window.:

var foo=myapp.helloWorld();
if (typeof foo == "string")
{
    foo=window[foo];
}

A little ugly, but will get me by until a better method comes along. Future Qt versions are going unify the scripting support so that it's all based on the JavaScriptCore in WebKit, so hopefully this will improve then!

旧伤还要旧人安 2024-07-29 04:23:08

您可以将对象指针分配给 QObject * 并返回它。

    QObject * obj = new MyObject();
    return obj;

这对我在 Linux 上的 Qt Webkit 端口有用。

You can assign your Object pointer to a QObject * and return that.

    QObject * obj = new MyObject();
    return obj;

That is working for me on Qt Webkit port on Linux.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文