Reduce String to visible/readable text only (C++, Qt)

47 views Asked by At

Hej Folks.
I cannot find an answer for my question, so apologize if that's a duplication.

I have a Qt Application and running a Qt::Process.
Furthermore, I show the stdout and stderr of the process to the user, so errors can be understood.
My approach is to append every output to a QString I can show in a Text QML Type.

    QObject::connect(update, &QProcess::readyRead, [update, this] () {
        QString newline = QString(update->readAll());
        m_updateLog.append(newline);
        emit newUpdateLog();
    });

The problem is that with a lot of \r (Carriage Return), the QString quickly gets pretty long.
I hoped to somehow reduce the length of the QString to his "readable" length?

Example : QString("Hello\nWorld\rUniverse") becomes QString("Hello\nUniverse")

I tried to write my own logic … but I'm kinda too stupid that it's work properly.
In the future I will cut the QString to a max length, but in cases with a lot of CR it gets kinda ugly,
because important informations will be cut :/.
( Example: QString("Loaded:\n0.4%\r0.5%\r0.6%\r0.7%\r0.8%\r0.9%\r1.0%") )

Thanks for any help.

1

There are 1 answers

0
Andrew McGuinness On

I wouldn't expect there to be any magic that understands that stuff before a "\r" is effectively erased -- that logic is in the text rendering implementation not any string formatting. You would have to implement it yourself; you would have to track where the "\r" is backing up the cursor to.

I would suggest replacing your single string (whatever type m_updateLog is) with a list or array of strings, with each line of output (delimited by "\r" or "\n") as its own string. That way when you see a "\r" you can drop the last element. Also if the array gets too long, you can remove the first elements to shorten it, so that you have a kind of rolling tail of the output.