[英]QML call method from C++ object
Ok, I know how to call C++ method from QML, but in this case it is not working. 好的,我知道如何从QML调用C ++方法,但是在这种情况下它不起作用。
This is how my code looks like (its not actual code since it is very big): 这是我的代码的样子(因为它很大,所以不是实际的代码):
Container //this is my main qml element, where is everything contained and started
{
ElemA
{
id: elemA
...
}
ElemB
{
id: elemB
...
}
}
ElemA
{
MyClass
{
id: myClass
...
}
//myClass.method() //runs fine
//Now I call: elemB.myClass = myClass
//after that, if I call method() it doens't work (below is code)
}
ElemB
{
property MyClass myClass
//myClass.method() //doesn't run at all, neither shows any errors or warnings
}
ElemA and ElemB are basically states, from which I switch. ElemA和ElemB基本上是状态,我从中切换。 It goes like this: everything starts in state ElemA, then I can switch into ElemB, but when I switch, method() is not working (as I said above, no errors or warnings).
它是这样的:一切都从状态ElemA开始,然后我可以切换到ElemB,但是当我切换时,method()无法正常工作(如上所述,没有错误或警告)。
I am pretty sure that something goes wrong (or maybe is not allowed, but somehow it passes) when I put "elemB.myClass = myClass", in other words, that I cannot pass C++ object as property to some other element (withing QML). 我很确定当我放入“ elemB.myClass = myClass”时,出了点问题(或者也许是不允许的,但是以某种方式通过了), 换句话说,我不能将C ++对象作为属性传递给其他元素(使用QML) )。
So my question is how I am supposed to solve this? 所以我的问题是我应该如何解决呢?
EDIT: My code is game actually. 编辑:我的代码实际上是游戏。 I have many Elem's, each Elem represents one state (for example, ElemMenu is game menu, when I press some button like "Options", it switches me to ElemOptions state where I can do some other things).
我有很多Elem,每个Elem代表一个状态(例如,ElemMenu是游戏菜单,当我按下“ Options”之类的按钮时,它将切换到ElemOptions状态,在这里我可以做其他事情)。
MyClass is C++ implementation for networking. MyClass是用于网络的C ++实现。 Now problem is that I need to have one instance of c++ object between 2 states (networking):
现在的问题是,我需要在2个状态(联网)之间有一个c ++对象实例:
Now once I accept enough client connections, I want to start game and switch to state ElemB but I need to have same object so I can manipulate with connected clients. 现在,一旦我接受了足够的客户端连接,我就想开始游戏并切换到状态ElemB,但是我需要具有相同的对象,以便可以与连接的客户端进行操作。
I have tried to inject myClass using this method: viewer.rootContext()->setContextProperty("myClass", new MyClass()); 我试图使用这种方法注入myClass: viewer.rootContext()-> setContextProperty(“ myClass”,new MyClass()); but then I am unable to use "myClass" with Connections element in QML (says that myClass property is undefined, but I can do method invocation like myClass.something() .
但后来我无法在QML中将“ myClass”与Connections元素一起使用(说myClass属性是未定义的,但是我可以像myClass.something()那样进行方法调用。
What I am thinking right now to reimplement MyClass, and make it static. 我现在想的是重新实现MyClass,并使它静态。 Then I could make in ElemA and ElemB instances of MyClass, but I would rather not if possible.
然后,我可以在MyClass的ElemA和ElemB实例中进行创建,但是如果可能的话,我宁愿不要这样做。
QtMobility is not option for me right now. QtMobility暂时不适合我。
I am using QT 1.8.1 with QtCreator 2.5 我在QtCreator 2.5中使用QT 1.8.1
Why not setting an alias on your property on elemB : 为什么不在elemB的属性上设置别名:
ElemB
{
property alias myClassInElemB: myClass
MyClass
{
id: myClass
...
}
}
In your code, you can do this : elemB.myClassInElemB = myClass
. 在您的代码中,您可以这样做:
elemB.myClassInElemB = myClass
。
However, I think there is a problem of conception in your code. 但是,我认为您的代码中存在概念问题。 Why do you use just like
ElemA
and ElemB
objects instead of the states
property of your Container
? 为什么你使用就像
ElemA
和ElemB
对象,而不是该states
的财产Container
? Why do you call elemB.myClass = myClass
in ElemA.qml (source code of ElemA
) ? 你为什么叫
elemB.myClass = myClass
在ElemA.qml(源代码ElemA
)? Solving this problem may enable you to write more simple things and avoid this problem with myClass
. 解决此问题可能使您能够编写更简单的内容,并避免使用
myClass
此问题。 Can you tell us more about your code ? 您能告诉我们更多有关您的代码的信息吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.