qt 获取每行行数和字符串

QString qstrAll("");
QString qstrPlainText = ui->editUnit->toPlainText();
QTextDocument* pDocument = ui->editUnit->document();
QTextBlock block = pDocument->begin();
while (block.isValid() && block.layout())
{
	QString qstrBlock = block.text();
	QTextLayout* pLayout = block.layout();
	int ctBlockLine = pLayout->lineCount();
	for (int i = 0; i < ctBlockLine; ++i)
	{
		QTextLine line = pLayout->lineAt(i);
		int nStart = line.textStart();
		int nLen = line.textLength();
		QString qstrBlockLine = qstrBlock.mid(nStart, nLen);
		qstrAll += qstrBlockLine;
	}
	block = block.next();
}

此代码帮我了大忙,如果对document不熟悉的话,就很难想到。

一般控件中都会包含document这个类。

你可能感兴趣的:(qt,qt,开发语言)