Qt 编程继续为大家讲解,还是接着文章 Qt 编程点滴 初学者必看 (6) ,继续介绍,说编程那些细节。由于本话题是一节一节为大家介绍的,所以更多内容请看末尾编辑推荐。本文基本是用代码实现的效果,所以并没有太多的内容。
QPainterPath 画出的图形会闪烁的问题:
用下面的写法画出的图形会闪烁
- class MyClass: public QWidget
- {
- public:
- MyClass(QWidget*);
- private:
- QPainterPath* route;
- void paintEvent(QPaintEvent*e);
- };
- MyClass::MyClass()
- {
- route = new QPainterPath();
- }
- void MyClass::paintEvent( QPaintEvent*e)
- {
- QPainter *painter = new QPainter(this);
- //画校正图形
- int insideR = 30;
- int outsideR = 50;
- QColor insideColor(237,29,12); //内圆线条颜色
- QColor outSideColor(237,29,12); //外圆线条颜色
- QColor lineColor(237,29,12); //直线颜色
- QColor insideBrushColor(255,0,0,25);//内圆画刷颜色,最后的参数代表透明度( 0(完全透明)-100(不透明) )
- QColor outsideBrushColor(255,0,0,50);//外圆画刷颜色,最后的参数代表透明度( 0(完全透明)-100(不透明) )
- //QPainterPath path;
- route->moveTo(insideR,0);
- route->lineTo(outsideR,0);
- route->arcTo(0-outsideR,0-outsideR,outsideR*2,outsideR*2,0,180);
- route->lineTo(0-insideR,0);
- route->arcTo(0-insideR,0-insideR,insideR*2,insideR*2,0,180);
- route->moveTo(0-insideR,0);
- route->lineTo(0-outsideR,0);
- route->arcTo(0-outsideR,0-outsideR,outsideR*2,outsideR*2,180,180);
- route->lineTo(insideR,0);
- route->arcTo(0-insideR,0-insideR,insideR*2,insideR*2,180,180);
- painter->setPen(Qt::NoPen);
- painter->setBrush(outsideBrushColor);
- painter->drawPath(*route);
- painter->setBrush(Qt::NoBrush);
- painter->setPen(outSideColor);
- painter->drawEllipse( QPointF(0,0),outsideR,outsideR );
- painter->setBrush(insideBrushColor);
- painter->setPen(insideColor);
- painter->drawEllipse( QPointF(0,0),insideR,insideR );
- painter->setPen(lineColor);
- QPoint p1(0, 0- outsideR - 10 );
- QPoint p2(0, outsideR + 10 );
- painter->drawLine(p1,p2);
- painter->rotate(90);
- painter->drawLine(p1,p2);
- painter->rotate(-90);
- delete painter;
- }
如下用下面的写法则不会闪烁:
- void MyClass::paintEvent( QPaintEvent*e)
- {
- QPainter *painter = new QPainter(this);
- //画校正图形
- int insideR = 30;
- int outsideR = 50;
- QColor insideColor(237,29,12); //内圆线条颜色
- QColor outSideColor(237,29,12); //外圆线条颜色
- QColor lineColor(237,29,12); //直线颜色
- QColor insideBrushColor(255,0,0,25);//内圆画刷颜色,最后的参数代表透明度( 0(完全透明)-100(不透明) )
- QColor outsideBrushColor(255,0,0,50);//外圆画刷颜色,最后的参数代表透明度( 0(完全透明)-100(不透明) )
- QPainterPath path;
- path.moveTo(insideR,0);
- path.lineTo(outsideR,0);
- path.arcTo(0-outsideR,0-outsideR,outsideR*2,outsideR*2,0,180);
- path.lineTo(0-insideR,0);
- path.arcTo(0-insideR,0-insideR,insideR*2,insideR*2,0,180);
- path.moveTo(0-insideR,0);
- path.lineTo(0-outsideR,0);
- path.arcTo(0-outsideR,0-outsideR,outsideR*2,outsideR*2,180,180);
- path.lineTo(insideR,0);
- path.arcTo(0-insideR,0-insideR,insideR*2,insideR*2,180,180);
- painter->setPen(Qt::NoPen);
- painter->setBrush(outsideBrushColor);
- painter->drawPath(path);
- painter->setBrush(Qt::NoBrush);
- painter->setPen(outSideColor);
- painter->drawEllipse( QPointF(0,0),outsideR,outsideR );
- painter->setBrush(insideBrushColor);
- painter->setPen(insideColor);
- painter->drawEllipse( QPointF(0,0),insideR,insideR );
- painter->setPen(lineColor);
- QPoint p1(0, 0- outsideR - 10 );
- QPoint p2(0, outsideR + 10 );
- painter->drawLine(p1,p2);
- painter->rotate(90);
- painter->drawLine(p1,p2);
- painter->rotate(-90);
- delete painter;
- }
小结:本文主要介绍了在Qt 窗体的使用,通过Qt 编程点滴介绍,也给自己提高了编程过程中需要注意的细节问题,由于本话题是一节一节为大家展现的,所以更多内容,请看编辑推荐。