Qt 编程点滴 初学者必看 (7)

移动开发
本文介绍的是Qt 编程点滴,作为一名新手,我建议必须看一看。编程那些事,只有编程人员自己明白!所以推荐本文。

Qt 编程继续为大家讲解,还是接着文章 Qt 编程点滴 初学者必看 (6) ,继续介绍,说编程那些细节。由于本话题是一节一节为大家介绍的,所以更多内容请看末尾编辑推荐。本文基本是用代码实现的效果,所以并没有太多的内容。

QPainterPath 画出的图形会闪烁的问题:

用下面的写法画出的图形会闪烁

  1. class MyClass: public QWidget  
  2. {  
  3. public:  
  4.   MyClass(QWidget*);  
  5. private:  
  6.     QPainterPath* route;  
  7.     void paintEvent(QPaintEvent*e);  
  8. };  
  9. MyClass::MyClass()  
  10. {  
  11.   route = new QPainterPath();  
  12. }  
  13. void MyClass::paintEvent( QPaintEvent*e)  
  14. {  
  15.     QPainter *painter = new QPainter(this);  
  16.     //画校正图形  
  17.     int insideR = 30;  
  18.     int outsideR = 50;  
  19.     QColor insideColor(237,29,12); //内圆线条颜色  
  20.     QColor outSideColor(237,29,12); //外圆线条颜色  
  21.     QColor lineColor(237,29,12); //直线颜色  
  22.     QColor insideBrushColor(255,0,0,25);//内圆画刷颜色,最后的参数代表透明度( 0(完全透明)-100(不透明) )  
  23.     QColor outsideBrushColor(255,0,0,50);//外圆画刷颜色,最后的参数代表透明度( 0(完全透明)-100(不透明) )  
  24.  
  25.     //QPainterPath path;  
  26.     route->moveTo(insideR,0);  
  27.     route->lineTo(outsideR,0);  
  28.     route->arcTo(0-outsideR,0-outsideR,outsideR*2,outsideR*2,0,180);  
  29.     route->lineTo(0-insideR,0);  
  30.     route->arcTo(0-insideR,0-insideR,insideR*2,insideR*2,0,180);  
  31.     route->moveTo(0-insideR,0);  
  32.     route->lineTo(0-outsideR,0);  
  33.     route->arcTo(0-outsideR,0-outsideR,outsideR*2,outsideR*2,180,180);  
  34.     route->lineTo(insideR,0);  
  35.     route->arcTo(0-insideR,0-insideR,insideR*2,insideR*2,180,180);  
  36.     painter->setPen(Qt::NoPen);  
  37.     painter->setBrush(outsideBrushColor);  
  38.     painter->drawPath(*route);  
  39.  
  40.     painter->setBrush(Qt::NoBrush);  
  41.     painter->setPen(outSideColor);  
  42.     painter->drawEllipse( QPointF(0,0),outsideR,outsideR );  
  43.     painter->setBrush(insideBrushColor);  
  44.     painter->setPen(insideColor);  
  45.     painter->drawEllipse( QPointF(0,0),insideR,insideR );  
  46.     painter->setPen(lineColor);  
  47.     QPoint p1(0,  0- outsideR - 10  );  
  48.     QPoint p2(0,  outsideR + 10  );  
  49.     painter->drawLine(p1,p2);  
  50.     painter->rotate(90);  
  51.     painter->drawLine(p1,p2);  
  52.     painter->rotate(-90);  
  53.     delete painter;  

如下用下面的写法则不会闪烁:

  1. void MyClass::paintEvent( QPaintEvent*e)  
  2. {  
  3.     QPainter *painter = new QPainter(this);  
  4.     //画校正图形  
  5.     int insideR = 30;  
  6.     int outsideR = 50;  
  7.     QColor insideColor(237,29,12); //内圆线条颜色  
  8.     QColor outSideColor(237,29,12); //外圆线条颜色  
  9.     QColor lineColor(237,29,12); //直线颜色  
  10.     QColor insideBrushColor(255,0,0,25);//内圆画刷颜色,最后的参数代表透明度( 0(完全透明)-100(不透明) )  
  11.     QColor outsideBrushColor(255,0,0,50);//外圆画刷颜色,最后的参数代表透明度( 0(完全透明)-100(不透明) )  
  12.     QPainterPath path;  
  13.     path.moveTo(insideR,0);  
  14.     path.lineTo(outsideR,0);  
  15.     path.arcTo(0-outsideR,0-outsideR,outsideR*2,outsideR*2,0,180);  
  16.     path.lineTo(0-insideR,0);  
  17.     path.arcTo(0-insideR,0-insideR,insideR*2,insideR*2,0,180);  
  18.     path.moveTo(0-insideR,0);  
  19.     path.lineTo(0-outsideR,0);  
  20.     path.arcTo(0-outsideR,0-outsideR,outsideR*2,outsideR*2,180,180);  
  21.     path.lineTo(insideR,0);  
  22.     path.arcTo(0-insideR,0-insideR,insideR*2,insideR*2,180,180);  
  23.     painter->setPen(Qt::NoPen);  
  24.     painter->setBrush(outsideBrushColor);  
  25.     painter->drawPath(path);  
  26.     painter->setBrush(Qt::NoBrush);  
  27.     painter->setPen(outSideColor);  
  28.     painter->drawEllipse( QPointF(0,0),outsideR,outsideR );  
  29.     painter->setBrush(insideBrushColor);  
  30.     painter->setPen(insideColor);  
  31.     painter->drawEllipse( QPointF(0,0),insideR,insideR );  
  32.     painter->setPen(lineColor);  
  33.     QPoint p1(0,  0- outsideR - 10  );  
  34.     QPoint p2(0,  outsideR + 10  );  
  35.     painter->drawLine(p1,p2);  
  36.     painter->rotate(90);  
  37.     painter->drawLine(p1,p2);  
  38.     painter->rotate(-90);  
  39.     delete painter;  

小结:本文主要介绍了在Qt 窗体的使用,通过Qt 编程点滴介绍,也给自己提高了编程过程中需要注意的细节问题,由于本话题是一节一节为大家展现的,所以更多内容,请看编辑推荐。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-06-17 14:29:55

Qt

2011-06-17 15:06:14

Qt

2011-06-17 15:32:28

Qt

2011-06-17 14:12:32

Qt

2011-06-17 14:54:31

Qt

2011-06-17 15:44:25

Qt

2011-06-17 14:41:56

Qt

2011-06-17 15:37:42

Qt

2011-06-17 15:19:28

Qt

2011-09-16 09:38:19

Emacs

2011-06-27 14:56:46

Qt Designer

2011-09-08 10:38:37

Widget

2011-08-24 17:05:01

Lua

2013-04-23 10:51:15

Linux压缩

2011-08-04 18:01:07

IOS Cocoa Touc

2011-07-26 17:55:16

iPhone Runtime

2009-10-29 09:19:59

ADO.NET

2009-11-17 15:33:26

PHP数组元素

2009-10-22 16:46:03

VB.NET初步知识

2011-08-02 14:48:04

IOS 多任务
点赞
收藏

51CTO技术栈公众号