Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8431219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:47:43+00:00 2026-06-10T05:47:43+00:00

Ok, I know how to call C++ method from QML, but in this case

  • 0

Ok, I know how to call C++ method from QML, but in this case it is not working.

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. 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).

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).

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).

MyClass is C++ implementation for networking.
Now problem is that I need to have one instance of c++ object between 2 states (networking):

  • 1st state (ElemA) is waiting for clients connections and accepting them
  • 2nd state (ElemB) is playing game

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.


I have tried to inject myClass using this method: 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().

What I am thinking right now to reimplement MyClass, and make it static. Then I could make in ElemA and ElemB instances of MyClass, but I would rather not if possible.

QtMobility is not option for me right now.

I am using QT 1.8.1 with QtCreator 2.5

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:47:44+00:00Added an answer on June 10, 2026 at 5:47 am

    Why not setting an alias on your property on elemB :

    ElemB
    {
        property alias myClassInElemB: myClass
    
        MyClass
        {
            id: myClass
            ...
        }
    }
    

    In your code, you can do this : 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 ? Why do you call elemB.myClass = myClass in ElemA.qml (source code of ElemA) ? Solving this problem may enable you to write more simple things and avoid this problem with myClass. Can you tell us more about your code ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Yes I know I can call Java code from Scala; but that is pointless;
I'd like to know how to call a GWT static method from the parent
i need to call method from several classes, but i don't know how to
I would like to know if it is possible to call a method from
I like to know why dont we call servlet constructor instead of init method
I know I can call it whatever, but just like there is a convention
I have a conundrum, I do know how to call method from other activity..
I want to know how to call .cs method from javascript file in asp.net
I want to call a method from one class to another, don't know if
I tried to call this method from multiple threads trying to get the ID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.