Questions about the use of QML and C++ and their connection
-
I am wondering how I should use QML and C++.
Each has its own capabilities and advantages, and it is possible to connect them.I am currently using QML for screen displays and C++ for calculations and other processing.
However, I often end up with processes that call the same classes and functions over and over.
What connections or structures should be made to avoid inadvertent redefinitions? -
Can you give a short example? I don't understand what you mean with:
However, I often end up with processes that call the same classes and functions over and over.
What connections or structures should be made to avoid inadvertent redefinitions? -
Can you give a short example? I don't understand what you mean with:
However, I often end up with processes that call the same classes and functions over and over.
What connections or structures should be made to avoid inadvertent redefinitions? -
I'm only taking a wild guess here....
But if the tactic you're using for exposing the C++ object to QML involves something like this:
// assume: QQmlEngine* engine // assume: "this" is your custom C++ subtype of QObject that you expose to QML engine->rootContext()->setContextProperty( "myCustomViewModel", this );
Then if you are doing that each time you instantiate an instance of your type, then you would be overwriting the reference that your QML has for "myCustomViewModel".
This is all wild conjecture on my part, because we really need more code snippets to understand. However, my conjecture matches your real-life codebase, then the solution is to de-couple instantiation from
setContextProperty
. Then you could instantiate many objects, but onlysetContextProperty
on one to export that one to QML. -
@sierdzio
If I try to use a C++ class from QML.
If I use that class in a class on another C++, it will be initialized again.
In such a case, the class can no longer be used from QML.@egusa said in Questions about the use of QML and C++ and their connection:
@sierdzio
[...] it will be initialized again. [...]Well, that part is up to you, really :-) You can take the instance from QML (pass it via signal, for example), or you can use your C++ object as a singleton and do the initialization on C++ part. Or you can init your object in main.cpp, pass it to QML as context property, then pass the same pointer down your C++ code to where you need it. There are many ways to get around this problem.