4

嗨,我是 Qt 编程的新手,我想知道如何获取 QTextEdit 中每一行的大小。

更新
我需要获取文本中每一行(行)的宽度,而不是 QTextEdit 中所有文本的宽度。

4

1 回答 1

0

更新

如果您想以像素为单位获取文本的大小(宽和高)和每个字符串的长度QTextEdit,您可以执行以下操作:

// split all text into list of strings by separator '\n' (new line symbol)
QStringList strLst = ui->textEdit->toPlainText().split('\n');
// gather font metrics in QTextEdit
QFont textEditFont = ui->textEdit->font();
QFontMetrics fm(textEditFont);
foreach (QString str, strLst)
{
    int pixelsWide = fm.width(str);
    int pixelsHigh = fm.height();
    qDebug() << QString("Row: %1:\n\tsymbols count = %2,\tpixels wide = %3,"
             "\tpixels high = %4")
                .arg(str)
                .arg(str.length())
                .arg(pixelsWide)
                .arg(pixelsHigh);
}
于 2012-11-07T13:15:37.840 回答