Nominate our 2022 Qt Champions!

I want to change the line spacing for fonts or text

  • Hi, I want to draw blocks of text in a QGraphicsScene and I want to have variable line spacing. I can't find any functions to change this property of a font. Is this possible???

    Currently I draw each line of text separately and do the spacing myself... It would be nice if Qt could handle this kind of thing by being able to change the line spacing or leading or something.

    Is there a way to do this now with simple or rich text?
    How does one go about asking for a new feature like this to be added to Qt??


  • Lifetime Qt Champion


    If I understood you correctly (I suppose you are using a QGraphicsTextItem) you should have a look at QTextDocument, QTextCursor and related classes to achieve what you want.

  • Thank you, I am currently using a QGraphicsSimpleTextItem but I can change that to something else if needed. I will look at those classes.

  • Moderators

    Using QTextDocument (which i also recommend) you have to do this by yourself.
    Qt unfortunately doesn't provide an simple property for this:
    QTextDocument* doc = ...;
    QTextFrame * root = doc->rootFrame();
    QTextFrame::iterator it;
    for( it = root->begin(); !(it.atEnd()); ++it )
    QTextBlock block = fit.currentBlock();
    block.blockFormat().setLineHeight(5, QTextBlockFormat::LineDistanceHeight); //set spacing of 5 pixels

  • Thank you for the sample code raven-worx . I am looking at using this for my text items.

Log in to reply