Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Send text from QML to CPP as QString
Forum Update on Monday, May 27th 2025

Send text from QML to CPP as QString

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 744 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bracha
    wrote on 15 Jun 2023, 18:17 last edited by
    #1

    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.

    R 1 Reply Last reply 17 Jun 2023, 05:39
    0
    • B Bracha
      15 Jun 2023, 18:17

      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.

      R Offline
      R Offline
      Rajeesh_Raveendran
      wrote on 17 Jun 2023, 05:39 last edited by Rajeesh_Raveendran
      #2

      @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:

      1. Create C++ class with Q_INVOKABLE function with argument(any native type)
      2. create instance of the class and register it to QML's root context using setContextProperty() API
      3. 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
      B 1 Reply Last reply 18 Jun 2023, 10:29
      0
      • R Rajeesh_Raveendran
        17 Jun 2023, 05:39

        @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:

        1. Create C++ class with Q_INVOKABLE function with argument(any native type)
        2. create instance of the class and register it to QML's root context using setContextProperty() API
        3. 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
        B Offline
        B Offline
        Bracha
        wrote on 18 Jun 2023, 10:29 last edited by
        #3

        @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```
        R 1 Reply Last reply 19 Jun 2023, 05:52
        0
        • B Bracha
          18 Jun 2023, 10:29

          @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```
          R Offline
          R Offline
          Rajeesh_Raveendran
          wrote on 19 Jun 2023, 05:52 last edited by
          #4

          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)

          B 1 Reply Last reply 19 Jun 2023, 08:18
          1
          • R Rajeesh_Raveendran
            19 Jun 2023, 05:52

            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)

            B Offline
            B Offline
            Bracha
            wrote on 19 Jun 2023, 08:18 last edited by
            #5

            @Rajeesh_Raveendran
            Oops, I don't know how I missed it but you are right.
            Thanks, I figured out the problem!

            1 Reply Last reply
            0
            • B Bracha has marked this topic as solved on 20 Jun 2023, 06:48

            1/5

            15 Jun 2023, 18:17

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved