2
votes

Trying to display a text file in qml. The file size is about 3 megabytes. At the same time there are:

  • long opening of the form,
  • large waste of memory.

Tried use ScrollView, Flickable, Text and TextArea. How can these problems be avoided?

QML

ScrollView {
    id: idScrollView
    anchors {
        fill: parent
        margins: Dimensions.x(15)
    }
    Text {
        id: idContent
        anchors {
            left: parent.left
            right: parent.right
            rightMargin: Dimensions.x(20)
        }
        text: viewmodel.getLogText()
        font.pixelSize: Dimensions.y(10)
        wrapMode: Text.Wrap
    }
}

C++

QString MainViewModel::getLogText()
{
    const int maxSz = 1024 * 200;
    QString result;
    QFile file(ALog::filePath());
    if (file.open(QIODevice::ReadOnly))
    {
        if (file.size() > maxSz)
            file.seek(file.size() - maxSz);
        QByteArray arr = file.read(maxSz);
        result = QString::fromLatin1(arr);
        if (file.size() > maxSz)
            result = QString("Skip %1  Kb\n\n").arg((file.size() - maxSz)/1024) + result;
        file.close();
    }

    return result;
}
1
Just use TextEdit in readOnly mode.Marek R
Unfortunately, it doesn't help.pier_nasos
Try to put the file in the resource file or to open it from FS. May be the problem is long loading, not long rendering. What is viewmodel?folibis
can you provide code of viewmodel? Also you should use textDocument to achieve improvement. You can also move process of loading document to side tread - details of code needed.Marek R
Checked, getLogText function works very quickly. Added its current content. Now is given only 200 kilobytes of text. But it has a delay in opening and increase memory by about 70 megabytes (according to Windows Manager).pier_nasos

1 Answers

3
votes

Found a partial solution. It loads much faster and consumes several times less memory. Among the disadvantages-there is no possibility to convert Text to TextArea to be able to select the text to copy to the clipboard.

property variant stringList: null

function updateText() {
    stringList = viewmodel.getLogText().split('\n')
    idContentListView.positionViewAtEnd()
}

ListView {
    id: idContentListView
    model: stringList
    anchors {
        fill: parent
        margins: Dimensions.x(15)
    }
    delegate: Text {
        anchors {
            left: parent.left
            right: parent.right
        }
        text: model.modelData
        font.pixelSize: Dimensions.y(10)
        textFormat: Text.PlainText
        wrapMode: Text.Wrap
    }
    ScrollBar.vertical: ScrollBar {}
}