Search code examples
performanceqtqmlraspbianraspberry-pi2

Textarea slow for logging


I have a Qt application and I'd like to show some log. I use a TextArea. However, if the log is large or the events come too fast the GUI can't draw Textarea fast enough.

I have analyzed this problem with Qt Creator (QML Profiler) and if the log is large it takes 300 ms to draw the GUI. I use this software on a Raspberry Pi2.

Any ideas how to solve this? Should I use other QML controls? Thanks.

QML code:

TextArea {
    text: appHandler.rawCommunication
    readOnly: true        
}

C++ code:

Q_PROPERTY(QString rawCommunication READ rawCommunication WRITE setrawCommunication NOTIFY rawCommunicationChanged)

void setrawCommunication(QString val)
{
    val.append("\n");
    val.append(m_rawCommunication);
    m_rawCommunication = val;
    emit rawCommunicationChanged(m_rawCommunication);
}

Solution

  • Use a view, like ListView. They instantiate their delegates as needed, based on which data the view says it needs to show depending on the position the user is at in the list. This means that they perform much better for visualising large amounts of data than items like TextArea, which in your case is going to keep a massive, ever-growing string in memory.

    Your delegate could then be a TextArea, so you'd have one editable block of text per log line. However, if you don't need styling, I'd recommend going with something a bit lighter, like TextEdit. Taking it one step further: if you don't need editable text, use plain old Text. Switching to these might not make much of a difference, but if you're still seeing slowness (and have lots of delegates visible at a time), it's worth a try.