Solved Some odd statements in a QML project
-
As a c++ las than can be created locally inside a qml file
What please? I didn't get this part.
Or as a singleton, if you want to combine the above 2
If I figure out the above one then will go to study this one too. :)
I'm rather confused by how many ways we can use C++ in QML. I think the best way is to use C++ as the back-end and QML as the front-end part, like the example above.
can you show some code?
-
@tomy said in Some odd statements in a QML project:
As a c++ las than can be created locally inside a qml file
I'm talking about
qmlRegisterType
that allows you to expose a c++ class to qml and you than can create instances of it inside your qmlThere's
qmlRegisterSingletonType
that allowes for one single instance to be shared across c++ and qml (intanciated in qml the same way as the point prior)and there's
https://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty
that is a (in)c++ (created) instance exposed to your qml engine and can than be accessed from all qml files as a "global pointer"the
userName
property is part ofBackEnd
but you try to listen to it insideApplicationWindow
that won't workThis 2 ways should work:
BackEnd{ id:backend onUserNameChanged: console.log("Username changed", userName) }
or
//anywhere in your main.qml Connections{ target: backend onUserNameChanged: console.log("Username changed", userName) }
-
Thank you for your explanations.
I tried to know all three types of using C++ in QML and went for Docs. As usual, rather bewildering! :( Your explanations were much better.1- qmlRegisterType: instantiated and used in QML. As a C++ class we can have instances of it not only in QML files but also in another C++ file in that project. Right?
2- qmlRegisterSingletonType: For specific uses when we only need one single instance of the cpp class to also be shared for both C++ side and qml as well. Right?
3- ContextProperty: We use it when we want an instance a class, defined in cpp, to be used globally in both cpp and QML sides. Right?
-
actully, when you look at the cpp class, youβll see that itβs a function and the name of a property as well.
the part in qml accesses the property, and therfore invokes the setter function and not the getter function, which happens to have the same name as the property.They both "must" have the same name because in the property arguments, they are pointed to for write and read functions. Agree?
Also, in
Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
where is that first argument "username" (of type QString) used in the project, please? -
@tomy said in Some odd statements in a QML project:
Thank you for your explanations.
I tried to know all three types of using C++ in QML and went for Docs. As usual, rather bewildering! :( Your explanations were much better.1- qmlRegisterType: instantiated and used in QML. As a C++ class we can have instances of it not only in QML files but also in another C++ file in that project. Right?
2- qmlRegisterSingletonType: For specific uses when we only need one single instance of the cpp class to also be shared for both C++ side and qml as well. Right?
3- ContextProperty: We use it when we want an instance a class, defined in cpp, to be used globally in both cpp and QML sides. Right?
yes, on all cases
They both "must" have the same name because in the property arguments, they are pointed to for write and read functions. Agree?
Also, in Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged) where is that first argument "username" (of type QString) used in the project, please?
QString userName
defines the property type, (QString) and the name you have to use in qml to access ituserName
. In this case the name for the qml property has the same name as theREAD
function -
@J.Hilk
Very good. I changed that and now it's more clear.
Thanks. :) -
@tomy said in Some odd statements in a QML project:
Thank you dear Hilk. (Where do you get those imojies from?! hhhh β_β)
If your running Linux: emoji
-
@fcarney
Windows! :(
But thanks for your attention. -
@tomy said in Some odd statements in a QML project:
Windows! :(
Or, search Emojipedia and then copy + paste: https://emojipedia.org/search/?q=laughing
-
@JKSH
Thanks.
With this nice gift, it's time to close the thread and mark it solved. π