- 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 do I make text follow the line/curve and angle of the QPainterPath?
You can set a QTransform [doc.qt.nokia.com] on a QPainter [doc.qt.nokia.com] causing the painting to follow a transformation. The usual problem is that it’s easy to rotate using QTransform::rotate() [doc.qt.nokia.com], but since it rotates around origin which is in the top left corner, the coordinates will not match the painting done without a transformation. Using QTransform [doc.qt.nokia.com] translation, you can translate the positions back to the original position, and all of this in the name of trigonometry.
- #include <QtGui>
- #include <cmath>
- {
- public:
- Widget ()
- private:
- {
- int drawWidth = width() / 100;
- pen.setWidth(drawWidth);
- painter.setPen(pen);
- //draw the bezier curve
- painter.drawPath(path);
- //Make the painter ready to draw chars
- font.setPixelSize(drawWidth*2);
- painter.setFont(font);
- painter.setPen(pen);
- qreal percentIncrease = (qreal) 1/(hw.size()+1);
- qreal percent = 0;
- for ( int i = 0; i < hw.size(); i++ ) {
- percent += percentIncrease;
- qreal angle = path.angleAtPercent(percent);
- qreal rad =qreal(0.017453292519943295769)*angle; // PI/180
- // From the documentation:
- /**
- QTransform transforms a point in the plane to another point using the following formulas:
- x' = m11*x + m21*y + dx
- y' = m22*y + m12*x + dy
- **/
- // So the idea is to find the "new position of the character
- // After we apply the world rotation.
- // Then translate the painter back to the original position.
- qreal sina = std::sin(rad);
- qreal cosa = std::cos(rad);
- // Finding the delta for the penwidth
- // Don't divide by 2 because some space would be nice
- qreal deltaPenX = cosa * pen.width();
- qreal deltaPenY = sina * pen.width();
- // Finding new posision after rotation
- qreal newX = (cosa * point.x()) - (sina * point.y());
- qreal newY = (cosa * point.y()) + (sina * point.x());
- // Getting the delta distance
- qreal deltaX = newX - point.x();
- qreal deltaY = newY - point.y();
- // Applying the rotation with the translation.
- painter.setWorldTransform(tran);
- }
- }
- };
- int main(int argc, char **argv)
- {
- Widget widget;
- widget.show();
- return app.exec();
- }

4 comments
June 12, 2012
Robot Herder
A slightly less mathematical approach gives Qt more of the work. Replace the paintEvent() for loop with:
August 3, 2012
Lab Rat
And it’s a normal text without spaces
August 3, 2012
Lab Rat
length is the old percent of course
August 3, 2012
Lab Rat
so: