Briefly
We've explained the basic drawing of QT graphics, including: Drawing text, lines, lines, rectangles, arcs, ellipses, polygons, pictures, and other advanced uses such as gradients, transformations, and so on.
In this section we will explain the drawing of the text in detail. Implemented primarily through the Qpainter Darwtext () function, which contains several overloaded functions, where you can specify the area to draw by Qrect, or you can specify the starting point of the drawing by Qpoint.
The Qfont class can assist in setting text size, weight, character spacing, and so on, and then use SetFont () to set it.
The Qfontinfo class is used to obtain information about the font, which can be obtained through the FontInfo () function.
You can also use more other helper classes to achieve better results, such as: Qtextoption, Qfontmetrics. where qtextoption can wrap text and set the effect of line wrapping and text display orientation. Qfontmetrics can calculate the length of text and perform special processing such as: Display ... )。
- Briefly
- Basic drawing
- Text position
- Not fully displayed
- Effect
- Source
- Qtextoption
- Qfontmetrics
- Font
Basic drawing Effect
Source
void MainWindow::p aintevent (qpaintevent *event) {q_unused (event);Qpainterpainter (This);//Set Brush colorPainter. Setpen(Qcolor (0, .) );//Draw area is the entire area of the current interface (default-the upper left corner starts)Painter. DrawText(rect (),qstringliteral ("one go, two or three Miles"));//Draw area starts at x coordinate 100,y coordinates 100Painter. DrawText(100, 100,qstringliteral ("Youth not old, struggle not only!") "));//Draw area starts at the coordinate point (20, 200)Painter. DrawText(Qpoint (a) ,qstringliteral ("pure open source beauty, fun, fun, reliable ... "));}
DrawText the overloaded function comparison, here we first introduce several common drawing methods.
Text position effect
Source
void MainWindow::paintEvent(QPaintEvent *event){ Q_UNUSED(event); painter(this); // 设置画笔颜色 painter.setPen(QColor(0, 160, 230)); // 绘制区域为当前界面的整个区域(居中) painter.drawText(rect(), Qt::AlignCenter, QStringLiteral("一去丶二三里"));}
From the front, no matter which method we use, we first need to control the drawing area of the text-qrect or the starting point qpoint, and then you can help control the display of the position based on the alignment-left, right, center, and so on.
Not fully displayed
Next, let's take a look at a display that is not complete.
Effect
Source
void MainWindow::paintEvent(QPaintEvent *event){ Q_UNUSED(event); painter(this); // 设置画笔颜色 painter.setPen(QColor(0, 160, 230)); painter.drawText(rect(), Qt::AlignCenter, QStringLiteral("青春不老,奋斗不止!-纯正开源之美,有趣、好玩、靠谱。。。"));}
Sometimes we have a similar situation, the display is not complete, then we are very upset, it's okay, we can use the text option qtextoption to wrap, or you can use Qfontmetrics to display ....
Qtextoption effect
Source
void mainwindow:: PaintEvent (qpaintevent *event) {q_unused (event); Qpainter painter (this) ; //set brush color painter.setpen (qcolor (0 , 160 , 230 ) ); Qtextoption option (Qt::alignleft | Qt::alignvcenter) ; option .setwrapmode (Qtextoption::wordwrap); Painter.drawtext (rect () , qstringliteral ( "Youth not old, struggle not only!") -Pure Open source beauty, fun, fun, reliable ... ) , option );
Text is too long, qtextoption can help us to break the line, you can also set the alignment, in addition, you can also set the line wrapping and text display direction and other effects.
Qfontmetrics effect
Source
voidMainWindow::p aintevent(qpaintevent*Event) {q_unused (event); qpainter painter (this);//Set Brush colorPainter.Setpen (Qcolor (0, the, the)); Qfontmetrics FM=Painter.FontMetrics (); QString StrText=Qstringliteral ("Youth is not old, struggle not only!" -Pure Open source beauty, fun, fun, reliable ... "); QString Strelidedtext=Fm.Elidedtext (StrText, Qt:: Elideright, $Qt:: Textshowmnemonic); Painter.DrawText (Rect (), Qt:: AlignCenter, Strelidedtext);}
If the text is too long, we do not want to break the line, just want to omit some of them as ..., then we can achieve through qfontmetrics. Here, when the length exceeds 200px, the right side of the text is set ....
Font effects
Source
voidMainWindow::p aintevent (Qpaintevent *Event) {q_unused (Event); qpainter painter ( This); Qtransform transform; Transform.rotate ( $);//Set Brush colorPainter.setpen (Qcolor (0, the, the)); Qfont font; Font.setfamily ("Microsoft Yahei");//SizeFont.setpointsize ( -);//ItalicFont.setitalic (true);//Set underlineFont.setunderline (true);//Set the underlineFont.setoverline (true);//Set letter caseFont.setcapitalization (Qfont::smallcaps);//Set character spacingFont.setletterspacing (Qfont::absolutespacing, -);//Use fontsPainter.setfont (font);//Get font informationQfontinfo info = Painter.fontinfo (); Info.family (); Info.italic ();//ConversionPainter.settransform (transform); Painter.drawtext (Rect (), Qstringliteral ("one go, two or three miles"));}
Through Qfont we can easily set the size of text, font spacing and other effects, and then according to FontInfo () to obtain font information qfontinfo. Of course, we can also add more effects, such as: through the qtransform to achieve the conversion, here we rotated 45 degrees.
QT Graphics (drawing text)