Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drawing a wrapped, justified text inside a rectangle and adjusting line spacing
Forum Updated to NodeBB v4.3 + New Features

Drawing a wrapped, justified text inside a rectangle and adjusting line spacing

Scheduled Pinned Locked Moved General and Desktop
qpainterqfontmetricsqtextdocumentline spacing
1 Posts 1 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pippin
    wrote on last edited by Pippin
    #1

    Hello,

    I am currently working on a project where I have to draw a text inside a rectangle (it's always the same rectangle at the same place on the same image), however the text varies. Sometimes it's a small text and it can be stored in it without adjusting the font size, and sometimes it goes out of the rectangle, and the size has to be decreased until it doesn't go out anymore. (DrawingTool is a QPainter, QLore is a QString, and LORE_FONT is a QFont.)

    	qreal size = 14.;
    	LORE_FONT.setPointSizeF(size);
    	QFontMetrics LoreMetrics(LORE_FONT);
    	QRect foo = LoreMetrics.boundingRect(34, 475, 350, 75, Qt::AlignJustify | Qt::TextWordWrap, QLore);
    	
    	while (foo.height() > 75)
    	{
    		size -= .02;
    		LORE_FONT.setPointSizeF(size);
    		QFontMetrics LoreMetrics(LORE_FONT);
    		foo = LoreMetrics.boundingRect(34, 475, 350, 75, Qt::AlignJustify | Qt::TextWordWrap, QLore);
    	}
    	
    	DrawingTool.setFont(LORE_FONT);
    	DrawingTool.drawText(QRectF(34, 475, 350, 75), QLore);
    

    I wrote this code and it works, any text is always inside the box. My problem is that the spacing between (wrapped) lines isn't high enough and it is a bit hard to read the text. I wanted to fix this and, since I didn't find any way to change that with QPainters, I read about QTextDocuments, but I'm not sure at all how this would play out.

    Is there an easy way to fix my problem?
    If not, do QTextDocument::rootFrame() and QFontMetrics::boundingRect(...) represent the same thing?
    Qt::AlignJustify seems to have no effect on my computer, anybody else using Ubuntu have the same bug?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved