- All (478)
- jom (0)
- Qt Linguist (7)
- Qt Eclipse Integration (9)
- Qt Designer (7)
- Qt Creator (4)
- Qt build system: qmake (31)
- Qt build system: configure (3)
- Qt Assistant (5)
- Printing (4)
- Porting from Qt 3 to Qt 4 (1)
- Plugins (7)
- Qt Visual Studio AddIn (2)
- Qt/MFC Migration (2)
- QtScript (3)
- MDI (2)
- XML (1)
- Widgets (22)
- WebKit (5)
- Tools and Containers (2)
- Threads (2)
- Text Handling (10)
- SQL (6)
- QtTest (1)
- QtService (1)
- Platform: Windows (49)
- Platform: Unix (16)
- Platform: Mac OS X (18)
- Image Formats (2)
- I/O (2)
- Graphicsview (8)
- Font handling (9)
- Event System (18)
- Drag and Drop (4)
- Dialogs (6)
- Desktop integration (3)
- ActiveQt (3)
- Itemviews (60)
- Layout (4)
- Qt Quick (10)
- Qt SDK (1)
- Licensing (2)
- Platform: Embedded Linux (38)
- Painting (32)
- OpenGL (4)
- Object Model (6)
- Network (5)
- Multimedia (3)
- Miscellanous (23)
- Main Window (19)
- Look and Feel (23)
- Development (0)
- Getting Involved (0)
- Routines (0)
How can I draw vertical text?
Drawing vertical text can be done in several ways. The easiest way might be to create a QTextDocument [doc.qt.nokia.com] in the paintEvent() [doc.qt.nokia.com] and use setHtml() [doc.qt.nokia.com] to set the text and specify the
- <br>
tag to get line breaks. Another way could be to iterate over the QChars [doc.qt.nokia.com] in the QString [doc.qt.nokia.com] and position them vertically in the paintEvent() yourself. Then you would need to calculate the height between each element.
If what you want is rotated text, then you can simply rotate the painter 90 degrees.
Finally, if you are not going to draw the text yourself, then a QLabel [doc.qt.nokia.com] can easily be used to display vertical text, simply by specifying “\n” after each character.
The example below illustrates some of the possibilities:
- #include <QtGui>
- {
- public:
- {}
- {
- #if 1
- QTextDocument document;
- document.setHtml("<br>T<br>e<br>s<br>t<br>");
- document.drawContents(&p);
- #else
- drawRotatedText(&p, 90, width() / 2, height() / 2, "The vertical text");
- #endif
- }
- {
- painter->save();
- painter->translate(x, y);
- painter->rotate(degrees);
- painter->drawText(0, 0, text);
- painter->restore();
- }
- };
- int main(int argc, char **argv)
- {
- Widget w;
- w.resize(200,200);
- QLabel label;
- label.setText(string);
- label.show();
- w.show();
- return a.exec();
- }

1 comment
December 5, 2010
Ant Farmer
The above is demonstrating a full application with rotated (any angle) text as well as some newline methods.
If it’s just vertically painted text you want, this function will suffice:
If you have a QPainter subclass, you can easily attach this to it and remove the painter argument so it appears like a normal drawText.