如何在 QWebView 中获取 Javascript 来创建 C++ 的新实例 基础课程?
我已使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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
尝试将新对象作为 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.
这个答案是基于保罗的答案,但有点简化。 已针对 Qt 5.3 进行测试和工作。 您需要一个工厂来实例化一个对象,然后返回一个指向该对象的 QObject 指针。 该对象的类必须继承自
QObject
,以便它在 JavaScript 中正常工作:有了这个,您可以从 Javascript 执行以下操作:
此时,您也有了
obj1
作为全局 JavaScript 命名空间中的myobject
(它们只是指针,因此执行以下两项工作:此时,您可以在 JavaScript 中使用
connect
将 C++ 信号连接到JavaScript 插槽。有关此技术的更多信息,请访问:http://doc.qt.io /qt-5/qtwebkit-bridge.htmlThis 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 fromQObject
so that it works properly in JavaScript:With this, you can do the following from Javascript:
At this point, you have
obj1
as well asmyobject
in the global JavaScript namespace (they are just pointers, so doing both of the following works: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我考虑过的一个相当丑陋的黑客方法是使用 addToJavaScriptWindowObject 将我想要返回的对象放入具有随机名称的窗口对象中,然后让我的插槽返回对象实例的名称:
可以编写 JS 来检查是否返回值是一个字符串,如果是,则从窗口中抓取该对象。:
有点难看,但会帮助我,直到出现更好的方法。 未来的 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:
The JS can be written to check if the return value is a string, and if it is, grab the object from the window.:
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!
您可以将对象指针分配给 QObject * 并返回它。
这对我在 Linux 上的 Qt Webkit 端口有用。
You can assign your Object pointer to a QObject * and return that.
That is working for me on Qt Webkit port on Linux.