Get data from QTextEdit line by line in QT

11.9k Views Asked by At

enter image description here

hi, I would like to get information from TextEdit in Qt line by line and write it in vector. How it is possible to do thanks. Would like to get vectorarr = {"{9,1,6,6}","{0,4,3,11}","{3,22,8,33}","{11,3,8,3}"};

2

There are 2 best solutions below

0
On BEST ANSWER

You can get All QTextEdit text and split it by \n (new line).

Get QTextEdit text:

QString data = ui->textEdit->toPlainText();

Split it to by \n (new line):

QStringList strList = data.split(QRegExp("[\n]"),QString::SkipEmptyParts);

Screenshot:

Image

0
On
QString QTextStream::readLine(qint64 maxlen = 0)

For reading text you can use QTextStream

    QString text = ui->lineEdit->text();
    QTextStream * stream = new QTextStream(&text , QIODevice::ReadOnly);
    QString line1 =  stream->readLine();
    QString line2 =  stream->readLine();

    qDebug() <<line1;

For adding it into vector

    QString text = ui->lineEdit->text();
    QTextStream * stream = new QTextStream(&text , QIODevice::ReadOnly);
    QVector<QString > lines;
    while (!stream->atEnd())
    {
        lines << stream->readLine();
    }