<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Send text from QML to CPP as QString]]></title><description><![CDATA[<p dir="auto">Hello,<br />
I have a program that contains logic in CPP and a GUI in QML<br />
I need to send variables received by the GUI user from QML to CPP<br />
I registered the CPP class to be a recognized Singleton in QML by calling its functions in QML.<br />
It works fine until the following glitch:<br />
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<br />
What is the correct way to send a string and receive it as a QSring?<br />
I tried sending text, string, text.toString(), string.toString,<br />
Get as Qvariant and convert in CPP, nothing.<br />
Every time I print the content I get something like this:<br />
Output_QMLTYPE_21(0x2c2d50)<br />
I couldn't find any info about it online and GPT didn't help me either.<br />
please help me.</p>
]]></description><link>https://forum.qt.io/topic/145802/send-text-from-qml-to-cpp-as-qstring</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 01:39:22 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/145802.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 15 Jun 2023 18:17:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Send text from QML to CPP as QString on Mon, 19 Jun 2023 08:18:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rajeesh_raveendran">@<bdi>Rajeesh_Raveendran</bdi></a><br />
Oops, I don't know how I missed it but you are right.<br />
Thanks, I figured out the problem!</p>
]]></description><link>https://forum.qt.io/post/761881</link><guid isPermaLink="true">https://forum.qt.io/post/761881</guid><dc:creator><![CDATA[Bracha]]></dc:creator><pubDate>Mon, 19 Jun 2023 08:18:19 GMT</pubDate></item><item><title><![CDATA[Reply to Send text from QML to CPP as QString on Mon, 19 Jun 2023 05:52:45 GMT]]></title><description><![CDATA[<p dir="auto">Did you confirm that your properties are populated correctly in QML itself?</p>
<p dir="auto">continueBtn.onClicked: {<br />
console.log(ipString)<br />
console.log(maskString)<br />
console.log(gatewayString)<br />
TechnicalController.setIP(ipString, maskString, gatewayString)</p>
]]></description><link>https://forum.qt.io/post/761863</link><guid isPermaLink="true">https://forum.qt.io/post/761863</guid><dc:creator><![CDATA[Rajeesh_Raveendran]]></dc:creator><pubDate>Mon, 19 Jun 2023 05:52:45 GMT</pubDate></item><item><title><![CDATA[Reply to Send text from QML to CPP as QString on Sun, 18 Jun 2023 10:29:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rajeesh_raveendran">@<bdi>Rajeesh_Raveendran</bdi></a><br />
thanks for the detailed answer.<br />
I did something similar without the third step - Q_PROPERTY<br />
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.<br />
It worked for me with an INT variable.<br />
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))<br />
I tried converting the text to a QML string variable without success.<br />
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.<br />
I will briefly describe what I do.<br />
There is a screen for the user to set the IP address, MASK and GATEWAY for the device.<br />
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.</p>
<p dir="auto">QML: (I write only the relevant code sections)</p>
<pre><code>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)
</code></pre>
<p dir="auto">Header file - TechnicalController :</p>
<pre><code>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&lt;TechnicalController &gt;("Globals", 1, 0, "TechnicalController ", InstantiateTechnicalController );
}
</code></pre>
<p dir="auto">main.cpp:</p>
<pre><code>calling to registerTechnicalControllerSingleton() while launching the app```</code></pre>
]]></description><link>https://forum.qt.io/post/761785</link><guid isPermaLink="true">https://forum.qt.io/post/761785</guid><dc:creator><![CDATA[Bracha]]></dc:creator><pubDate>Sun, 18 Jun 2023 10:29:18 GMT</pubDate></item><item><title><![CDATA[Reply to Send text from QML to CPP as QString on Sat, 17 Jun 2023 05:40:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bracha">@<bdi>Bracha</bdi></a> 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?</p>
<p dir="auto">Usually when we want to access value from QML to CPP following method can be followed:<br />
1st method:</p>
<ol>
<li>Create C++ class with Q_INVOKABLE function with argument(any native type)</li>
<li>create instance of the class and register it to QML's root context using setContextProperty() API</li>
<li>then access it from QML call the API from QML pass the arguments. Now you can handle it in C++<br />
2nd method:<br />
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</li>
</ol>
]]></description><link>https://forum.qt.io/post/761742</link><guid isPermaLink="true">https://forum.qt.io/post/761742</guid><dc:creator><![CDATA[Rajeesh_Raveendran]]></dc:creator><pubDate>Sat, 17 Jun 2023 05:40:31 GMT</pubDate></item></channel></rss>