Important: Please read the Qt Code of Conduct -

[solved] return a qstring of a line by lineposition of a plaintextedit

  • I'm hoping to load a text file and parse it line by line to read in [token's] kind of xml like.

    I thought the anchorAt returned a QString of a line.

    I thought I could use


    To get the # of lines of the plainTextEdit.

    Apparently that is not the case.

    Is there a way I can read a single line as a QString from a plainTextEdit?

    Can I send it to another container/datatype and process it more easily in another data container?

    I have noticed a textCursor function that has the ability to select. Is this what I should be using? I'd hate to do all that selecting myself in front of the user's eyes. I guess I could hide the form?

    I think I can do it by sending the data to a qtextstream and following the steps outlined here

    QString holderString = ui->plainTextEditLeft->toPlainText();

    QTextStream in(&holderString);
    //count lines
    int lineCount = 1;
    *QString line* = in.readLine();
    while (!in.atEnd())
        line = in.readLine();
    //resets QTextStream cursor;

  • Try put text from QTextEdit to QStringList and than you may take any row which you want. After processing of QStringList, you may put all together and place back to QTextEdit
    QString plainTextEditContents = ui->textEdit->toPlainText();
    QStringList lines = plainTextEditContents.split("\n");
    //some processing

    //join all in one string
    for (int i = 0; i < lines.size(); ++i) {
    plainTextEditContents +=;
    if (i!=lines.size()-1)
    plainTextEditContents += "\n";

  • thanks, I'll backupdate to that for faster access.

Log in to reply