[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

    ->blockCount();

    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?

    Note:
    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?

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

    http://stackoverflow.com/questions/15115571/reading-a-txt-file-using-qtextstream-c

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

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


  • 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
    plainTextEditContents.clear();
    for (int i = 0; i < lines.size(); ++i) {
    plainTextEditContents += lines.at(i);
    if (i!=lines.size()-1)
    plainTextEditContents += "\n";
    }
    ui->textEdit->setText(plainTextEditContents)
    @



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.