0

Windows 7 SP1
MSVS 2010
Qt 4.8.4

このコードを考えると:

#include <QTGui>

int main(int argc, char *argv[])
{
    QTextDocument* text_document = new QTextDocument("testing");
    QTextBlock text_block = text_document->begin();
    qDebug() << text_block.text() << text_block.blockFormat().lineHeight()
             << text_block.blockFormat().lineHeightType();
}

コンソールには次のように表示されます。

"testing" 0 0

質問:lineHeightが「段落のLineHeightプロパティ」を返さないのはなぜですか?lineHeightTypeはシングルスペースに設定されています。

私は明らかにこれを理解していません。出力する前に行の高さを設定しようとしても、何も起こりません(lineHeight()はまだゼロです):

text_block.blockFormat().setLineHeight(30,QTextBlockFormat::SingleHeight);

明確にするために、私のアプリケーションでは、GUIウィンドウに出力するときに何も起こりません。

試しても:

 qDebug() << text_block.text() << text_block.layout()->boundingRect().height();

私にゼロを与えます。

4

2 に答える 2

0

これは、ウィジェットがshow()関数を呼び出す前または後に行われますか?IE、それらは表示されますか?これまでQTextBlockを使用したことはありませんが、すべてが表示されるまで(または、イベントループが開始されるまで)、QWidgetsのサイズを信頼できないことがわかりました。これはmain()にも当てはまります。

デバッグの目的で、アプリケーションの実行後にそれらをチェックします。

于 2013-02-01T00:06:59.837 に答える
0

lineHeightが機能することはありませんが、これは機能します。

int CalculateLineHeight(QTextBlock text_block)
{
    int max_ascent  = 0;
    int max_descent = 0;
    int max_leading = 0;

    // A fragment is a piece of the text block with the the same format, such as font.
    for (QTextBlock::Iterator fragment_it = text_block.begin(); !(fragment_it.atEnd()); ++fragment_it)
    {
        QTextFragment fragment = fragment_it.fragment();
        QTextCharFormat fragment_format = fragment.charFormat();
        QFont fragment_font = fragment_format.font();
        QFontMetrics fragment_font_metrics (fragment_font);
        max_ascent  = std::max(fragment_font_metrics.ascent(), max_ascent);
        max_descent = std::max(fragment_font_metrics.descent(),max_descent);
        // Find the leading of the font with the maximum height. If more than
        // one, then find the largest lead among them.
        if ( current_height > max_height )
        {
            max_height = current_height;
            max_leading = current_leading;
        }
        else if ( current_height == max_height && current_leading > max_leading )
        {
            max_leading = current_leading;
        }
    }
    return max_ascent + max_descent + max_leading + 1; // + 1 for the baseline
}

答えはmaxheight()だと思うかもしれませんが、同じ高さのフォントは、上昇、下降、および進みが異なる場合があります。

于 2013-02-04T20:38:02.450 に答える