问题
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