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

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

最佳答案

更新

如果您想获得 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);
}

关于c++ - 获取 QTextEdit 中每一行的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13270016/

10-11 16:04