Get data from QTextEdit line by line in QT [closed]

◇◆丶佛笑我妖孽 提交于 2020-04-07 05:54:47

问题


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


回答1:


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:




回答2:


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


来源:https://stackoverflow.com/questions/46254245/get-data-from-qtextedit-line-by-line-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!