Send text from QML to CPP as QString
-
wrote on 15 Jun 2023, 18:17 last edited by
Hello,
I have a program that contains logic in CPP and a GUI in QML
I need to send variables received by the GUI user from QML to CPP
I registered the CPP class to be a recognized Singleton in QML by calling its functions in QML.
It works fine until the following glitch:
I need to send a text message. And it doesn't come as a QString to CPP, no matter how many variations of variable types and calls to toString() I try
What is the correct way to send a string and receive it as a QSring?
I tried sending text, string, text.toString(), string.toString,
Get as Qvariant and convert in CPP, nothing.
Every time I print the content I get something like this:
Output_QMLTYPE_21(0x2c2d50)
I couldn't find any info about it online and GPT didn't help me either.
please help me. -
Hello,
I have a program that contains logic in CPP and a GUI in QML
I need to send variables received by the GUI user from QML to CPP
I registered the CPP class to be a recognized Singleton in QML by calling its functions in QML.
It works fine until the following glitch:
I need to send a text message. And it doesn't come as a QString to CPP, no matter how many variations of variable types and calls to toString() I try
What is the correct way to send a string and receive it as a QSring?
I tried sending text, string, text.toString(), string.toString,
Get as Qvariant and convert in CPP, nothing.
Every time I print the content I get something like this:
Output_QMLTYPE_21(0x2c2d50)
I couldn't find any info about it online and GPT didn't help me either.
please help me.wrote on 17 Jun 2023, 05:39 last edited by Rajeesh_Raveendran@Bracha its not clear how you are passing value from QML to CPP. is it by calling a Q_INVOKABLE function written on your registered C++ class?
Usually when we want to access value from QML to CPP following method can be followed:
1st method:- Create C++ class with Q_INVOKABLE function with argument(any native type)
- create instance of the class and register it to QML's root context using setContextProperty() API
- then access it from QML call the API from QML pass the arguments. Now you can handle it in C++
2nd method:
similar to 1st method except that instead of function, you have to define Q_PROPERTY in C++ class with WRITE method mentioned. So that when you assign value to the property in QML, write function will get invoked with the assigned value
-
@Bracha its not clear how you are passing value from QML to CPP. is it by calling a Q_INVOKABLE function written on your registered C++ class?
Usually when we want to access value from QML to CPP following method can be followed:
1st method:- Create C++ class with Q_INVOKABLE function with argument(any native type)
- create instance of the class and register it to QML's root context using setContextProperty() API
- then access it from QML call the API from QML pass the arguments. Now you can handle it in C++
2nd method:
similar to 1st method except that instead of function, you have to define Q_PROPERTY in C++ class with WRITE method mentioned. So that when you assign value to the property in QML, write function will get invoked with the assigned value
wrote on 18 Jun 2023, 10:29 last edited by@Rajeesh_Raveendran
thanks for the detailed answer.
I did something similar without the third step - Q_PROPERTY
I already did it in another function and it works well - simply call the function from the QML with the variables that need to be sent, and in C++ the function receives and processes them as needed.
It worked for me with an INT variable.
The problem is with variables of type STRING, I send TEXT from QML and it is received as a certain type of QML and not as QSTRING (I see it in the printout, I print the received variable and the output is something like Output_QMLTYPE_21( 0x2c2d50))
I tried converting the text to a QML string variable without success.
Q_PROPERTY is relevant if I need to send 3 strings to the function? Or do you need three times? It seems to me that it is a bit cumbersome and unnecessary, but maybe in this situation there is no choice.
I will briefly describe what I do.
There is a screen for the user to set the IP address, MASK and GATEWAY for the device.
All three are saved as text in the GUI and when the user clicks "Continue" the data must be sent to the CPP for necessary processing.QML: (I write only the relevant code sections)
UpdateIPWindowForm { property string ipString: ip.text.toString() property string maskString: mask.text.toString() property string gatewayString: gateway.text.toString() continueBtn.onClicked: { TechnicalController.setIP(ipString, maskString, gatewayString)
Header file - TechnicalController :
class TechnicalController : public QObject { Q_OBJECT public: Q_INVOKABLE setIP(QString, QString, QString) } inline TechnicalController * TechnicalController ::getInstance() { static TechnicalController *singleInstance = new TechnicalController (); return singleInstance; } inline QObject *InstantiateTechnicalController (QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) return TechnicalController ::getInstance(); } inline void registerTechnicalControllerSingleton() { qmlRegisterSingletonType<TechnicalController >("Globals", 1, 0, "TechnicalController ", InstantiateTechnicalController ); }
main.cpp:
calling to registerTechnicalControllerSingleton() while launching the app```
-
@Rajeesh_Raveendran
thanks for the detailed answer.
I did something similar without the third step - Q_PROPERTY
I already did it in another function and it works well - simply call the function from the QML with the variables that need to be sent, and in C++ the function receives and processes them as needed.
It worked for me with an INT variable.
The problem is with variables of type STRING, I send TEXT from QML and it is received as a certain type of QML and not as QSTRING (I see it in the printout, I print the received variable and the output is something like Output_QMLTYPE_21( 0x2c2d50))
I tried converting the text to a QML string variable without success.
Q_PROPERTY is relevant if I need to send 3 strings to the function? Or do you need three times? It seems to me that it is a bit cumbersome and unnecessary, but maybe in this situation there is no choice.
I will briefly describe what I do.
There is a screen for the user to set the IP address, MASK and GATEWAY for the device.
All three are saved as text in the GUI and when the user clicks "Continue" the data must be sent to the CPP for necessary processing.QML: (I write only the relevant code sections)
UpdateIPWindowForm { property string ipString: ip.text.toString() property string maskString: mask.text.toString() property string gatewayString: gateway.text.toString() continueBtn.onClicked: { TechnicalController.setIP(ipString, maskString, gatewayString)
Header file - TechnicalController :
class TechnicalController : public QObject { Q_OBJECT public: Q_INVOKABLE setIP(QString, QString, QString) } inline TechnicalController * TechnicalController ::getInstance() { static TechnicalController *singleInstance = new TechnicalController (); return singleInstance; } inline QObject *InstantiateTechnicalController (QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) return TechnicalController ::getInstance(); } inline void registerTechnicalControllerSingleton() { qmlRegisterSingletonType<TechnicalController >("Globals", 1, 0, "TechnicalController ", InstantiateTechnicalController ); }
main.cpp:
calling to registerTechnicalControllerSingleton() while launching the app```
wrote on 19 Jun 2023, 05:52 last edited byDid you confirm that your properties are populated correctly in QML itself?
continueBtn.onClicked: {
console.log(ipString)
console.log(maskString)
console.log(gatewayString)
TechnicalController.setIP(ipString, maskString, gatewayString) -
Did you confirm that your properties are populated correctly in QML itself?
continueBtn.onClicked: {
console.log(ipString)
console.log(maskString)
console.log(gatewayString)
TechnicalController.setIP(ipString, maskString, gatewayString)wrote on 19 Jun 2023, 08:18 last edited by@Rajeesh_Raveendran
Oops, I don't know how I missed it but you are right.
Thanks, I figured out the problem! -
1/5