<?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[canReadLine() working with &#x2F;n and &#x2F;r]]></title><description><![CDATA[<p dir="auto">Hello. I am parsing incoming serial data using signal and socket:</p>
<pre><code> connect(&amp;serial_local-&gt;serial_connection, &amp;QSerialPort::readyRead, this, &amp;Widget::readData);
</code></pre>
<pre><code>
void Widget::readData()
{

      while(serial_local-&gt;serial_connection.canReadLine()){
        if(ui-&gt;autoscroll-&gt;isChecked()){
            QTextCursor cursor = ui-&gt;Console_read-&gt;textCursor();
            cursor.clearSelection();
            cursor.movePosition(QTextCursor::End);
            ui-&gt;Console_read-&gt;setTextCursor(cursor);
        }

        QScrollBar *scrollbar = ui-&gt;Console_read-&gt;verticalScrollBar();
        bool scrollbarAtBottom  = (scrollbar-&gt;value() &gt;= (scrollbar-&gt;maximum() - 4));
        int scrollbarPrevValue = scrollbar-&gt;value();

        QTextCursor cursor = ui-&gt;Console_read-&gt;textCursor();
        cursor.clearSelection();
        cursor.movePosition(QTextCursor::End);
        ui-&gt;Console_read-&gt;setTextCursor(cursor);

        QByteArray line = serial_local-&gt;serial_connection.readLine();
        QString DataAsString = QString(line);
        logging_local-&gt;Write_to_file(DataAsString);

        //All other data with ANSI code is printed in black color

            ui-&gt;Console_read-&gt;setTextColor(Qt::white); //by default the text will be black
            ui-&gt;Console_read-&gt;insertPlainText(DataAsString);
        
        //reposition the cursor once the write is complete
        if(!ui-&gt;autoscroll-&gt;isChecked())
        {
            if (scrollbarAtBottom)
            {
                qDebug("ensure cursor visible \n");
                ui-&gt;Console_read-&gt;ensureCursorVisible();
            }
            else
            {
                ui-&gt;Console_read-&gt;verticalScrollBar()-&gt;setValue(scrollbarPrevValue);
            }
        }

    }

}

</code></pre>
<p dir="auto">Everything works fine except for one issue:<br />
canReadLine does not work with \r (only works with \n). Is there any way to override this function to work with both \r and \n ?</p>
<p dir="auto">I have tried to use readAll() but for my particular application I must use canReadLine.</p>
]]></description><link>https://forum.qt.io/topic/139243/canreadline-working-with-n-and-r</link><generator>RSS for Node</generator><lastBuildDate>Mon, 26 Oct 2026 16:27:29 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139243.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 12 Sep 2022 06:28:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Wed, 14 Sep 2022 08:04:10 GMT]]></title><description><![CDATA[<p dir="auto">Makes sense. Thank you very much for clarifying. I read a little bit about  printf in the ESP32 documentation (external device that I have connected)<br />
<a href="https://esp32.com/viewtopic.php?t=3447" target="_blank" rel="noopener noreferrer nofollow ugc">https://esp32.com/viewtopic.php?t=3447</a></p>
]]></description><link>https://forum.qt.io/post/728650</link><guid isPermaLink="true">https://forum.qt.io/post/728650</guid><dc:creator><![CDATA[lukutis222]]></dc:creator><pubDate>Wed, 14 Sep 2022 08:04:10 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Wed, 14 Sep 2022 07:48:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lukutis222">@<bdi>lukutis222</bdi></a><br />
Quite possibly along the lines that @J-Hilk has said.  <em>When</em> do you think whatever your "device" is flushes stdout?  Maybe at a <code>\n</code>, but why should it flush at a <code>\r</code>?  Either put in a <code>fflush(stdout)</code> if you want flushing (e.g. before/at the start of each delay), or set stdout to unbuffered if you want that, or use <code>write(1, ...)</code> instead of <code>printf()</code> if you don't want to deal with stdout buffering?</p>
]]></description><link>https://forum.qt.io/post/728648</link><guid isPermaLink="true">https://forum.qt.io/post/728648</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 14 Sep 2022 07:48:47 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Wed, 14 Sep 2022 07:36:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lukutis222">@<bdi>lukutis222</bdi></a> said in <a href="/post/728645">canReadLine() working with /n and /r</a>:</p>
<blockquote>
<p dir="auto">printf("new message2 \r");</p>
</blockquote>
<p dir="auto">I bet if you try:</p>
<pre><code>while(1){
        printf("hello message1 \r");
        printf("hello message2 \n");
        delay(1000);
        printf("new message1 \r");
        printf("new message2 \n");
        delay(1000);
    }
</code></pre>
<p dir="auto">You will see on your side perfectly expected behaviour.</p>
<p dir="auto">Seems like your device is only actually writing to / flushing the serialport when there is<br />
a) a new line character<br />
b) some (probably fixed) amount of time has passed since last flush and new data is available to send</p>
]]></description><link>https://forum.qt.io/post/728646</link><guid isPermaLink="true">https://forum.qt.io/post/728646</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Wed, 14 Sep 2022 07:36:30 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Wed, 14 Sep 2022 07:32:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">I was testing a little bit more and I notice a strange behavior. Perhaps you have any ideas why would the following happen? I use the following code:</p>
<pre><code>

void Widget::readData()
{

    // save the scrollbar position
    QScrollBar *scrollbar = ui-&gt;Console_read-&gt;verticalScrollBar();
    bool scrollbarAtBottom  = (scrollbar-&gt;value() &gt;= (scrollbar-&gt;maximum() - 4));
    int scrollbarPrevValue = scrollbar-&gt;value();

    // this moves the cursor to the bottom to avoid writing data in the middle of the console
    QTextCursor cursor = ui-&gt;Console_read-&gt;textCursor();
    cursor.clearSelection();
    cursor.movePosition(QTextCursor::End);
    ui-&gt;Console_read-&gt;setTextCursor(cursor);




        QByteArray data = serial_local-&gt;serial_connection.readAll();
        QString DataAsString = QString(data);
        DataAsString.replace("\r", "\n");
        QStringList myStringList = DataAsString.split("\n");
        for(int i =0;i &lt; myStringList.length();i++)
        {
            //All other data with ANSI code is printed in black color
                ui-&gt;Console_read-&gt;setTextColor(Qt::white); //by default the text will be black
                ui-&gt;Console_read-&gt;insertPlainText(DataAsString);
                break;
        }
    

    if (scrollbarAtBottom)
    {
        ui-&gt;Console_read-&gt;ensureCursorVisible();
    }
    else
    {
        ui-&gt;Console_read-&gt;verticalScrollBar()-&gt;setValue(scrollbarPrevValue);
    }


}
</code></pre>
<p dir="auto">I test 3 different scenarios:</p>
<p dir="auto"><strong>SCENARIO1</strong></p>
<p dir="auto">external device sending:</p>
<pre><code>while(1){
        printf("hello message1 \n");
        printf("hello message2 \n");
        delay(1000);
        printf("new message1 \n");
        printf("new message2 \n");
        delay(1000);
    }
</code></pre>
<p dir="auto">Everything seems to be working nicely. The received data is printed nicely on the console:</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/c295c2a5-70e9-4422-ad4f-9c59b05e73e9.png" alt="8fb82f07-94f8-464e-911f-b0fd9a2aee94-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto"><strong>SCENARIO2</strong></p>
<p dir="auto">external device sending:</p>
<pre><code>while(1){
        printf("hello message1 \n");
        printf("hello message2 \n");
        delay(1000);
        printf("new message1 \r");
        printf("new message2 \r");
        delay(1000);
    }
</code></pre>
<p dir="auto">Something a little strange is starting to happen although all data is being printed in the console. I am expecting to receive 2 lines then 1 second delay, then another 2 lines and another 1 second delay(this is what happens in <strong>Scenario1</strong>), however that is not the case. All 4 lines are printed at once every 2 seconds due to adding \r.</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/b19c3776-60aa-4d20-b2be-186762e5ca73.png" alt="39075ff7-0ad3-408a-9be3-89ca38d4f9ff-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto"><strong>SCENARIO3</strong></p>
<p dir="auto">external device sending:</p>
<pre><code>while(1){
        printf("hello message1 \r");
        printf("hello message2 \r");
        delay(1000);
        printf("new message1 \r");
        printf("new message2 \r");
        delay(1000);
    }
</code></pre>
<p dir="auto">Even stranger things happen when device is only printing with \r. The device seems to be printing multiple lines (6 to 8 lines) every 3-4 seconds. I am not sure what is happening:</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/992c8c2a-09d7-4521-b140-15660b3e75ae.png" alt="44a92ce4-5cb0-4400-a2af-7ad58ad8a4ea-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">It feels like readAll(); does not want to work properly with \r. Maybe you have some ideas?</p>
]]></description><link>https://forum.qt.io/post/728645</link><guid isPermaLink="true">https://forum.qt.io/post/728645</guid><dc:creator><![CDATA[lukutis222]]></dc:creator><pubDate>Wed, 14 Sep 2022 07:32:49 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Wed, 14 Sep 2022 05:22:04 GMT]]></title><description><![CDATA[<p dir="auto">It is a shame that QSerialPort::open() does not support QIODeviceBase::Text.</p>
]]></description><link>https://forum.qt.io/post/728631</link><guid isPermaLink="true">https://forum.qt.io/post/728631</guid><dc:creator><![CDATA[ChrisW67]]></dc:creator><pubDate>Wed, 14 Sep 2022 05:22:04 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Wed, 14 Sep 2022 04:31:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
Yeah.. I also want the terminal to be as much universal as possible. I want it to work with both devices that use \n , \r and even mixture of both.</p>
<p dir="auto">Your suggestion is quite clever :) It seems to work without any issues</p>
]]></description><link>https://forum.qt.io/post/728628</link><guid isPermaLink="true">https://forum.qt.io/post/728628</guid><dc:creator><![CDATA[lukutis222]]></dc:creator><pubDate>Wed, 14 Sep 2022 04:31:12 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Mon, 12 Sep 2022 09:57:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lukutis222">@<bdi>lukutis222</bdi></a> said in <a href="/post/728318">canReadLine() working with /n and /r</a>:</p>
<blockquote>
<p dir="auto">The device that I am monitoring is printing in both (\n and \r) so I need to be able to detect both.</p>
</blockquote>
<p dir="auto">That sounds pretty bad!  You show that lines can be <em>either</em> <code>\r</code> <em>or</em> <code>\n</code> terminated, never heard of such a device.  Anyway just adjust for that, it's just a simple coding requirement.  Your code is not good enough, as you can see (e.g. hint: if you want to "split", or detect line break, you need to look for whichever of <code>\r</code> or <code>\n</code> comes <em>first</em>).</p>
<p dir="auto">P.S.<br />
If you don't actually <em>care</em> whether a "line" arrived with <code>\r</code> or <code>\n</code> as terminator and want them treated the same, this might make it simpler:</p>
<pre><code>QString DataAsString = QString(data);
DataAsString.replace("\r", "\n");
QStringList myStringList = DataAsString.split("\n");
</code></pre>
]]></description><link>https://forum.qt.io/post/728321</link><guid isPermaLink="true">https://forum.qt.io/post/728321</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 12 Sep 2022 09:57:19 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Mon, 12 Sep 2022 09:28:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>jonB</bdi></a></p>
<p dir="auto">The device that I am monitoring is printing in both (\n and \r) so I need to be able to detect both.<br />
If I understand what you suggested correctly, you say that I can use readAll() to read all data and simply split string into different lines myself. I decided to try the following to split data with \n and \r:</p>
<pre><code>void Widget::readData2()
{
    if(ui-&gt;autoscroll-&gt;isChecked()){
        QTextCursor cursor = ui-&gt;Console_read_3-&gt;textCursor();
        cursor.clearSelection();
        cursor.movePosition(QTextCursor::End);
        ui-&gt;Console_read_3-&gt;setTextCursor(cursor);
    }

    QByteArray data = serial_local-&gt;serial_connection2.readAll();
    QString DataAsString = QString(data);



    QStringList myStringList1 = DataAsString.split("\n");
    for(int i =0;i &lt; myStringList1.length();i++)
    {
      qDebug("data split with n = %s \n",myStringList1.at(i).toStdString().c_str());
    }


    QStringList myStringList2 = DataAsString.split("\r");
    for(int i =0;i &lt; myStringList2.length();i++)
    {
      qDebug("data split with r= %s \n",myStringList2.at(i).toStdString().c_str());
    }


    //ui-&gt;Console_read_3-&gt;setTextColor(Qt::white); //by default the text will be black
    //ui-&gt;Console_read_3-&gt;insertPlainText(DataAsString);


}
</code></pre>
<p dir="auto">The external device that I am monitoring is simply doing the following:</p>
<pre><code>while(1){
      printf("hello message1 \n");
      printf("hello message2 \n");
      delay(1000); // delay 1 second
      printf("new message1 \r");
      printf("new message2 \r");
      delay(1000);
}
</code></pre>
<p dir="auto">The results checking application output logs:</p>
<pre><code>data split with n =  
data split with r= new message1  
data split with r= new message2  
data split with r= hello message1  
data split with r= 
hello message2  
data split with r= 
 
 
 
data split with n =  
data split with r= new message1  
data split with r= new message2  
data split with r= hello message1  
data split with r= 
hello message2  
data split with r= 
 
 
 
data split with n =  
data split with r= new message1  
data split with r= new message2  
data split with r= hello message1  
data split with r= 
hello message2  
data split with r= 
 
 
 
data split with n =  
data split with r= new message1  
data split with r= new message2  
data split with r= hello message1  
data split with r= 
hello message2  
data split with r= 
</code></pre>
<p dir="auto">The results are not quite as expect. I was expecting 2 lines with \n and 2 lines with \r to be printed but that is not really the caes</p>
]]></description><link>https://forum.qt.io/post/728318</link><guid isPermaLink="true">https://forum.qt.io/post/728318</guid><dc:creator><![CDATA[lukutis222]]></dc:creator><pubDate>Mon, 12 Sep 2022 09:28:39 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Mon, 12 Sep 2022 08:29:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> oh now I got ya!</p>
<p dir="auto">very true, I didn't bother check the code further, I only took the topic title by face value :D</p>
]]></description><link>https://forum.qt.io/post/728295</link><guid isPermaLink="true">https://forum.qt.io/post/728295</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Mon, 12 Sep 2022 08:29:51 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Mon, 12 Sep 2022 08:34:53 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk<br />
Please read my answer again :)  You show that <code>canReadLine()</code> is <code>virtual</code>, which it is, I said the problem is that <code>readLine()</code> is <em>not</em>!  So for example</p>
<pre><code>if/while (canReadLine())
    line = readLine();
</code></pre>
<p dir="auto">will/may not function as you would expect, which is the whole point of using it.</p>
<p dir="auto"><code>QIODevice::readLineData()</code> <em>is</em> virtual, and is used by <code>readLine()</code>.  The problem is that <code>readLineData()</code> <em>leaves</em> the <code>\n</code> ("it should return the number of bytes read in this line, including the terminating newline") and <code>readLine()</code> expects to see a <code>\n</code> at the end of the line, which won't be there....</p>
<p dir="auto">You may be able to make it work, would have to look at the Qt source code.  But seems a lot easier/safer to me to just implement <code>canReadLine</code>/<code>readLine()</code>-type methods, without overriding, which does what the OP wants with <code>\r</code>....</p>
]]></description><link>https://forum.qt.io/post/728292</link><guid isPermaLink="true">https://forum.qt.io/post/728292</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 12 Sep 2022 08:34:53 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Mon, 12 Sep 2022 08:04:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/728289">canReadLine() working with /n and /r</a>:</p>
<blockquote>
<p dir="auto">The problem is that QIODevice::readLine() is not virtual</p>
</blockquote>
<p dir="auto">according to the documentation, it is:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/2f755cf1-a312-40f0-b945-c2a4ca84e953.png" alt="b4ac369b-9bae-405d-a4a0-c9bb3e853d51-image.png" class=" img-fluid img-markdown" /><br />
<a href="https://doc.qt.io/qt-6/qiodevice.html#canReadLine" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qiodevice.html#canReadLine</a></p>
]]></description><link>https://forum.qt.io/post/728290</link><guid isPermaLink="true">https://forum.qt.io/post/728290</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Mon, 12 Sep 2022 08:04:09 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Mon, 12 Sep 2022 08:01:46 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk said in <a href="/post/728273">canReadLine() working with /n and /r</a>:</p>
<blockquote>
<p dir="auto">you can subclass  and override canReadLine, it's virtual.</p>
</blockquote>
<p dir="auto">You can, but it will likely take you down a blind alley!  The problem is that <code>QIODevice::readLine()</code> is <em>not</em> virtual, and it is fixed on reading up to <code>\n</code>.  This will lead to <code>canReadLine()</code> returning true when <code>readLine()</code> will not read the same line as that.  Not advisable.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lukutis222">@<bdi>lukutis222</bdi></a></p>
<blockquote>
<p dir="auto">I have tried to use readAll() but for my particular application I must use canReadLine.</p>
</blockquote>
<p dir="auto">If your data uses <code>\r</code> and no <code>\n</code> for "line" separation, I suggest you implement a little buffer code using <code>readAll()</code> to fill and do you own search/split on <code>\r</code>.  That is all the existing <code>readLine()</code> stuff does and is not hard/only a few lines of code.</p>
]]></description><link>https://forum.qt.io/post/728289</link><guid isPermaLink="true">https://forum.qt.io/post/728289</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 12 Sep 2022 08:01:46 GMT</pubDate></item><item><title><![CDATA[Reply to canReadLine() working with &#x2F;n and &#x2F;r on Mon, 12 Sep 2022 06:40:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lukutis222">@<bdi>lukutis222</bdi></a> said in <a href="/post/728270">canReadLine() working with /n and /r</a>:</p>
<blockquote>
<p dir="auto">canReadLine</p>
</blockquote>
<p dir="auto">It's always looking for a NewLine Character <code>\n</code> and not carriage return <code>\r</code> like the name suggests ;P<br />
<a href="https://doc.qt.io/qt-6/qiodevice.html#readLine" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qiodevice.html#readLine</a></p>
<p dir="auto">you can subclass  and override canReadLine, it's virtual.</p>
<pre><code>bool QIODevice::canReadLine() const
{
	    Q_D(const QIODevice);
	    return d-&gt;buffer.indexOf('\r', d-&gt;buffer.size(),
	                             d-&gt;isSequential() ? d-&gt;transactionPos : Q_INT64_C(0)) &gt;= 0;
}
</code></pre>
]]></description><link>https://forum.qt.io/post/728273</link><guid isPermaLink="true">https://forum.qt.io/post/728273</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Mon, 12 Sep 2022 06:40:00 GMT</pubDate></item></channel></rss>