pier_nasos
pier_nasos

Reputation: 678

Show big text in qml

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

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;
}

Upvotes: 2

Views: 1780

Answers (1)

pier_nasos
pier_nasos

Reputation: 678

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 {}
}

Upvotes: 3

Related Questions