View Full Version : How to draw vertical text via QTextEdit
nifei
24th February 2009, 04:52
hi, i need to draw text in both vertical layout and horizontal layout. like
aamer4yu
24th February 2009, 05:31
If your drawing yourself, you can rotate the painter , easiest way i guess :)
nifei
24th February 2009, 05:54
If your drawing yourself, you can rotate the painter , easiest way i guess :)
I want to draw the text from top to bottom on QTextDocument and call QTextCocument : : drawContents(QPainter *).
BTW, what is the relationship between QTextDocument, QTextEdit, QTextFormat and QTextTable/ QTextList , .etc? I'm new to these classes.
jryannel
24th February 2009, 08:46
As the previous poster said, use rotate the painter. Look in the analog clock example for how todo this.
In Qt instead of rotating/scaling (e.g. transforming) objects, we transform the painter. It's much easier like this as soon as you get used to;-)
This might also help you:
- http://doc.trolltech.com/qq/qq24-textlayouts.html
- Juergen
nifei
24th February 2009, 09:26
As the previous poster said, use rotate the painter. Look in the analog clock example for how todo this.
In Qt instead of rotating/scaling (e.g. transforming) objects, we transform the painter. It's much easier like this as soon as you get used to;-)
This might also help you:
- http://doc.trolltech.com/qq/qq24-textlayouts.html
- Juergen
Thanks for your suggestion and the linked article, that is a little complicated for my project. May be rotate QPainter is the only way to draw vertical text. I supposed to edit the whole text via QTextDocument and render them to QPainter, if rotating/translating the QPainter, all the text will be rotated or translated.
i think i have to draw the contents of QTextDocument twice, once drawing horizontal text, then rotating the QPainter and drawing the vertical text. is that right?
wagmare
24th February 2009, 09:41
Thanks for your suggestion and the linked article, that is a little complicated for my project. May be rotate QPainter is the only way to draw vertical text. I supposed to edit the whole text via QTextDocument and render them to QPainter, if rotating/translating the QPainter, all the text will be rotated or translated.
i think i have to draw the contents of QTextDocument twice, once drawing horizontal text, then rotating the QPainter and drawing the vertical text. is that right?
it is a little suggestion ... libqxt-0.4 is having the property to rotate your widget . especially pushButton , lineEdit ... etc ... in the designer level itself ..
get the source and add it to your qt library ..
nifei
24th February 2009, 10:04
it is a little suggestion ... libqxt-0.4 is having the property to rotate your widget . especially pushButton , lineEdit ... etc ... in the designer level itself ..
get the source and add it to your qt library ..
l don't think we can use libqxt-0.4 library...for me the key problem is not how to draw vertical text now, but how to draw both vertical and horizontal text in one time. i hope this is feasible.
nifei
24th February 2009, 10:20
I traced through QTextDocument : : drawContents (QPainter*) in hopes of QTextFormat's properties being used to control the painter, as i found QTextFormat : : setProperty ( int, QVariant) and guessed we could set QTextFormat : : UserProperty as vertical or horizontal then use this property when using the painter. however, i got lost.:(
My ideas is to re-implement QTextDocument : : drawContents (QPainter * ) or other functions to get the QTextFormat's properties that have been set by users, According to the UserProperty rotate the painter or not. is this a bad idea?
CustomTextDocument::someFunction(QPainter *painter, QTextFormat *format)
// format might be sub class of QTextFormat, for example, QBlockTextFormat
{
if (format.property(QTextFormat::UserProperty).toInt( ) == CustomTextDocument::Vertical)
{
painter->save();
painter->translate( xoff, yoff);// to correct the text's position
painter->rotate(90);
QTextDocument::someFunctioin(painter, format);
// call the basic class's function to draw the text, however rotated.
painter->restore();
}
else
QTextDocument::someFunction(painter, format);
// if no vertical text is needed, call basic class's function
}
steno
1st October 2010, 18:10
So I have this code snippet to draw vertical text in a QTextEdit. However, I have 2 problems with this code. The cursor doesn't update when moved, the cursor doesn't blink, and when text is selected it doesn't draw the selection. Any ideas?
void TextEdit::paintEvent(QPaintEvent *e)
{
QPainter painter(viewport());
painter.rotate(-90);
painter.translate(-viewport()->rect().height(),0);
QAbstractTextDocumentLayout::Selection selection;
selection.cursor = textCursor();
selection.format = textCursor().charFormat();
QAbstractTextDocumentLayout::PaintContext ctx;
ctx.cursorPosition = textCursor().position();
ctx.selections.append(selection);
document()->documentLayout()->draw(&painter,ctx);
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.