[Qt-interest] Text Layout woes
Josiah Bryan
jbryan at productiveconcepts.com
Fri Sep 18 17:08:22 CEST 2009
Jason H wrote:
> Can I please 'bump' this. I've been on the IRC channel, QtCentre, and no one seems to know...
> Thanks!
Jason -
I know the feeling - seems everyone else gets answers on theses lists
sometimes except for you, eh? :-) Anyway...
I'm not sure if this is what you're trying to do or not - but I had a
similar situation: I wanted to draw a QTextDocument, text wrapped to a
rectangle, with a custom pen and brush. But there's no way to spec a
custom pen and brush (that I know of) unless you paint the text document
yourself. And when you paint the doc yourself, you dont get the word
wrapping implicit in QTextLayout and friends. (At least, that's what
I've experienced - maybe someone can correct me - please? :-)
Anyway, my solution was to write my own text wrapping function for a
QTextDocument and layout the text myself. Source available on Google
Code. Link to google code's online subversion repo, right to the start
of the function in the source:
http://code.google.com/p/dviz/source/browse/trunk/src/items/TextBoxContent.cpp?spec=svn43&r=43#607
To see it in action, just checkout the trunk:
svn checkout http://dviz.googlecode.com/svn/trunk/ dviz-read-only
And do:
qmake
make
./dviz
By the way, I *gladly* welcome ANY improvements or fixes to that text
layout function - please let me know if you fix anything in it. The only
outstanding "bug" it has right now is that it doesn't adjust the
baseline properly for fonts with widely varying heights in the same
line. (E.g. a tall font and a short font on the same line - the tall
font will be aligned with the top of the short font, sinking below the
baseline. Anyway, thats to-be-fixed "sometime" - I welcome any help.)
To reuse, grab the updateTextConstraints() function and the
TextLineSpecs class def from TextBoxContent.h and the hack to fit your
needs. Should be fairly easy to adapt. (Oh, see the paint method just
above the updateTextConstraints() for the actual painting of the text.
Note that it draws the text using a QPainterPath, which is automatically
created by TextLineSpec in it's constructor.)
Hope this helps.
Cheers!
-josiah
> ----- Original Message ----
> From: Jason H <scorp1us at yahoo.com>
> To: qt-interest at trolltech.com
> Sent: Wednesday, September 16, 2009 11:57:00 PM
> Subject: [Qt-interest] Text Layout woes
>
> Hi I am tring to make an Affine Text Item for QGraphicsView.
>
>
> First, I rendered a single line of text with addText in a QPainterPath. My goal was to render a paragraph of text. QPainterPath doesn't do that. (Issue#1)
>
> Then I ditched that and just used drawText from QPainter. But I found there were pixel differences. Whereas the path version scales smoothly, the raw painter does not. (Issue #2)
>
> So then I figured I'd use QTextLayout, and I come to find that QTextLine doesn't actually give you the text that will fit. (Issue #3) If it did, I could use the line's coords and text with QPainterPath to make my paragraph.
>
> Issue #4, is that I can't just create a QPainter for my QPainterPath and do my operations on the painter, and get back a QPainterPath.
>
> I just want a long line of affine text to be wrapped within a given rect. It seems that we must use QPainterPaths (because of issue #2) in the implementation.
>
> So what is a boy to do?
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
--
-=-=-=-=-=-=-=-=-=-=-=-=-
Josiah Bryan
Productive Concepts, Inc.
jbryan at pciint.com
(765) 964-6009, ext. 224
More information about the Qt-interest-old
mailing list