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这个类。

12-13 02:22