2

I am working on an Android App. I need to display a console like type of log, which will be written to by the C++ back end. I have tried doing this combining a TextEdit and ScrollView, but the result is really slow. As soon as my log goes beyond ~50 lines, adding a few lines slows down (locks) the interface for a few seconds.

Trimming down the source code, this is the log view section:

property int logMaxLines: 50

ScrollView {
    id: logScrollView
    anchors.fill: parent
    clip: true
    ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
    TextEdit {
        id: logTextEdit
        anchors.fill: parent
        readOnly: true
        color: "darkgreen"
        property int linesTrimmed: 0
    }
}

Connections{
    target: gate
    onNewMessageLineAdded :
    {
        logTextEdit.append(gate.newMessageLine)
        if (logTextEdit.lineCount > logMaxLines) {
            while (logTextEdit.lineCount >= logMaxLines) {
                logTextEdit.text = logTextEdit.text.slice(logTextEdit.text.indexOf('\n')+2)
                logTextEdit.linesTrimmed++
            }
            logTextEdit.insert(0, "[... trimmed " + logTextEdit.linesTrimmed + " lines ...]\n")
        }
    }
}

I picked a ScrollView as I'd like to have the vertical scroll bar. Lines are added one at a time by the C++ code, when it emits the newMessageLineAdded signal. This is coming from a class which includes this Q_PROPERTY, used to pass the new line content:

Q_PROPERTY(QString newMessageLine READ newMessageLine NOTIFY newMessageLineAdded)

the signal is declared as:

void newMessageLineAdded();

I have added the small bit of java to trim the log when it grows too long, as the issue is there even when this trimming code is not present.

Am I doing something very clunky here? Should I use another type of object to replace the TextEdit, knowing that it is not used at all to edit text, but only as a display? Thanks.

Will59
  • 1,430
  • 1
  • 16
  • 37

1 Answers1

1

I recommend you to use ListView instead of TextEdit. And use QStringListModel as model declared in C++ code and added to QML as context property. Read Embedding C++ Objects into QML with Context Properties. It is recommended for better perfomance to have most of logic in C++ code.

ephemerr
  • 1,833
  • 19
  • 22
  • Thank you, I moved to QStringListModel and a ListView, I don't see any freezes anymore. – Will59 Feb 18 '18 at 17:44
  • In case you know, as a follow up question: each time I update my list content to append a line (using QStringList::append then QStringListModel::setStringList), my ListView scrolls back up to the top. Do you know if there is a way to preserve the scroll position, or what ListView property I should investigate? Thanks again for the answer. – Will59 Feb 18 '18 at 21:05
  • Or maybe better way: `Component.onCompleted: positionViewAtEnd()`. Look this answer for details: https://stackoverflow.com/questions/25815379/qml-listview-method-positionviewatend-does-exactly-the-opposite – ephemerr Feb 19 '18 at 06:25