我们可以在javascript中使用自定义的qbject派生类作为类型吗?

时间:2022-09-11 10:36:00

Let’s suposse I have a QObject-derived class:

我们假设我有一个qobject派生类:

Class Person : public QObject 
{
Q_OBJECT
Public:
QString _name;
Int _age;
}

Then at some place in my code I register this class into the context: …setContextProperty("_Person",new Person(qApp));

然后在代码中的某个地方,我将这个类注册到上下文中:…setContextProperty(“_Person”,new Person(qApp));

Is there any way to use as _Person as type in a javascript expression? Something like like:

在javascript表达式中是否有方法可以作为_Person类型使用?类似的:

function f() {
var p = // function that returns a _Person
p._name = "John Smith"
}

I know we can use the object created by new Person() inside a component through “_Person”, but what if I want to access _name and _age in a javacript function?

我知道我们可以通过“_Person”在组件中使用new Person()创建的对象,但是如果我想在javacript函数中访问_name和_age怎么办?

Thanks!

谢谢!

1 个解决方案

#1


0  

I believe you need to be defining _name and _age as properties.

我认为您需要将_name和_age定义为属性。

There are examples here and here.

这里和这里都有例子。

Note that the scriptable attribute will be required: -

注意,脚本属性将是必需的:-

The SCRIPTABLE attribute indicates whether this property should be accessible by a scripting engine (default true). Instead of true or false, you can specify a boolean member function.

脚本属性指示脚本引擎是否应该访问该属性(默认为true)。可以指定布尔成员函数,而不是true或false。

#1


0  

I believe you need to be defining _name and _age as properties.

我认为您需要将_name和_age定义为属性。

There are examples here and here.

这里和这里都有例子。

Note that the scriptable attribute will be required: -

注意,脚本属性将是必需的:-

The SCRIPTABLE attribute indicates whether this property should be accessible by a scripting engine (default true). Instead of true or false, you can specify a boolean member function.

脚本属性指示脚本引擎是否应该访问该属性(默认为true)。可以指定布尔成员函数,而不是true或false。