Qt中画弧/立体图形的类封装

开发环境:QT5.3 ,Window平台

用惯了Net的Graphics再过来用Qt的QPainter其实是很习惯的,尤其是画弧.,这里摘自我的一个交互式白板项目中画图形的部分代码

fakePainter.h/cpp

#include <QObject>
#include <QPointF>
#include <QSizeF>

class QPainter;
///
/// \brief The FakePainter class
/// 对QPainter的功能封装
///
class FakePainter
{
public:
    //绘制圆弧的方向/顺时针还是逆时针
    enum SweepDiretction{ Counterclockwise , Clockwise};

    ///
    /// \brief ArcTo
    /// \param painter
    /// \param point            绘制圆弧的起始点
    /// \param size             绘制圆弧的X,Y半径
    /// \param rotationAngle    角度(默认 0-360)
    /// \param sweepDirection   方向,clockwise顺时针 ,Counterclockwise逆时针
    /// 绘制圆弧
    ///
    static void ArcTo( QPainter* painter , const QPointF& point , const QSizeF& size , double rotationAngle , FakePainter::SweepDiretction sweepDirection );

    ///
    /// \brief ArcDashTo
    /// \param painter
    /// \param point
    /// \param size
    /// \param rotationAngle
    /// \param sweepDirection
    /// 绘制虚线风格的圆弧
    static void ArcDashTo(QPainter* painter , const QPointF& point , const QSizeF& size , double rotationAngle , FakePainter::SweepDiretction sweepDirection);

    static void DrawDashLine(QPainter* painter, const QPointF& p1 , const QPointF& p2 );
};
#include "fakepainter.h"
#include <QPainter>


void FakePainter::ArcTo(QPainter *painter, const QPointF &point, const QSizeF &size, double rotationAngle, FakePainter::SweepDiretction sweepDirection){
    double sweep = sweepDirection == Clockwise ? rotationAngle * 16 : -rotationAngle *16 ;


    painter->drawArc(QRectF(point.x() , point.y() - size.height(),size.width(),size.height()* 2  ), rotationAngle, sweep );
}

void FakePainter::ArcDashTo(QPainter *painter, const QPointF &point, const QSizeF &size, double rotationAngle, FakePainter::SweepDiretction sweepDirection)
{
    double sweep = sweepDirection == Clockwise ? rotationAngle * 16 : -rotationAngle *16 ;

    painter->save();
    QPen pen = painter->pen();
    pen.setStyle(Qt::DashLine);
    painter->setPen(pen);

    painter->drawArc(QRectF(point.x() , point.y() - size.height(),size.width(),size.height()* 2  ), rotationAngle, sweep );
    painter->restore();
}

void FakePainter::DrawDashLine(QPainter *painter, const QPointF &p1, const QPointF &p2){
    painter->save();

    QPen pen  =painter->pen();
    pen.setStyle(Qt::DashLine );
    painter->setPen(pen);

    painter->drawLine(QLineF(p1,p2));
    painter->restore();
}
View Code

 

 

部分立体图形绘制 figure3dcontext.h/cpp

#include <qobject.h>
#include <qpainter.h>
#include <QRectF>

///
/// \brief The Figure3DContext class
/// 提供快速绘制立体图形的方法
///
class Figure3DContext
{
public:
    Figure3DContext();

    ///绘制半球体
    static void DrawHemisphere(QPainter* painter ,  const QRectF& bound );
    ///绘制圆柱体
    static void DrawCylinder(QPainter* painter ,  const QRectF& bound );
    ///绘制正方体
    static void DrawCube(QPainter* painter ,  const QRectF& bound );
    ///绘制圆锥
    static void DrawCone(QPainter* painter , const QRectF& bound );
    ///绘制圆台柱体
    static void DrawRoundpillars(QPainter* paitner ,const QRectF& bound );
};
#include "figure3dcontext.h"
#include "fakepainter.h"
#include <QPainterPath>

Figure3DContext::Figure3DContext()
{
}

void Figure3DContext::DrawHemisphere(QPainter *painter, const QRectF &bound){
    qreal x = bound.x() ,y = bound.y();
    qreal w = bound.width() , h = bound.height();



    QPointF pt(x , y + h * 3 / 4);
    FakePainter::ArcTo(painter,pt , QSizeF(w , 3*h /4) ,  180, FakePainter::Clockwise );
    FakePainter::ArcDashTo(painter,pt, QSizeF(w ,h/4 ),180, FakePainter::Clockwise);
    FakePainter::ArcTo(painter,pt, QSizeF(w ,h/4 ),180, FakePainter::Counterclockwise);
}

void Figure3DContext::DrawCylinder(QPainter *painter, const QRectF &bound){
    QPainterPath path;
    qreal x = bound.x() ,y = bound.y();
    qreal w = bound.width() , h = bound.height();

    path.moveTo(x , y + h/7);
    path.lineTo(x, y + 6* h / 7 );
    path.moveTo(x+w , y+ h/7);
    path.lineTo(x + w , y + h * 6  / 7 );
    painter->drawPath( path );

    FakePainter::ArcTo(painter,QPointF(x , y + h/7),QSizeF(w, h/7), 180 , FakePainter::Clockwise);
    FakePainter::ArcTo(painter,QPointF(x , y + h/7),QSizeF(w, h/7), 180 , FakePainter::Counterclockwise);
    FakePainter::ArcDashTo(painter,QPointF(x, y + 6* h / 7 ),QSizeF(w, h/7), 180 , FakePainter::Clockwise);
    FakePainter::ArcTo(painter,QPointF(x, y + 6* h / 7 ),QSizeF(w, h/7), 180 , FakePainter::Counterclockwise);
}

void Figure3DContext::DrawRoundpillars(QPainter *painter, const QRectF &bound){
    QPainterPath path;
    qreal x = bound.x() ,y = bound.y();
    qreal w = bound.width() , h = bound.height();
    path.addEllipse(QRectF(QPointF(x+w/4 , y ), QPointF(x+w*3/4,y+2*h/10)));
    path.moveTo(x+w/4 , y + h/10);
    path.lineTo(x, y+ h*9/10 );
    path.moveTo(w+x, y+ h*9/10);
    path.lineTo(x+w*3/4, y + h/10);
    painter->drawPath(path);


    FakePainter::ArcDashTo(painter,QPointF(x , y + h*9/10),QSizeF(w,h/10),180,FakePainter::Clockwise);
    FakePainter::ArcTo(painter,QPointF(x , y + h*9/10),QSizeF(w,h/10),180,FakePainter::Counterclockwise);

}

void Figure3DContext::DrawCone(QPainter *painter, const QRectF &bound){
    QPainterPath path;
    qreal x = bound.x() ,y = bound.y();
    qreal w = bound.width() , h = bound.height();

    path.moveTo(x+w/2 ,y );
    path.lineTo(x , y + h*6/7);
    path.moveTo(x+w/2, y );
    path.lineTo(x+ w , y + h*6/7);
    painter->drawPath( path );

    //绘制底部
    FakePainter::ArcDashTo(painter,QPointF(x , y+h*6/7),QSizeF(w ,  h/7) , 180 , FakePainter::Clockwise );
    FakePainter::ArcTo(painter,QPointF(x , y+h*6/7),QSizeF(w ,  h/7) , 180 , FakePainter::Counterclockwise );
}

void Figure3DContext::DrawCube(QPainter *painter, const QRectF &bound){
    QPainterPath path;
    qreal x = bound.x() ,y = bound.y();
    qreal w = bound.width() , h = bound.height();
    path.moveTo(x , y+ h/3);
    path.lineTo(x , y + h );
    path.lineTo(x+w*2/3 , y + h );
    path.lineTo(x + w , y + 2*h/3);
    path.lineTo(x + w , y );
    path.lineTo(x + w *2 / 3 ,y + h /3 );
    path.lineTo(x , y + h /3 );
    path.lineTo(x+ w/3,y);
    path.lineTo(x+w, y );
    path.moveTo( x+ w*2/3, y + h/3);
    path.lineTo(x+2*w/3  ,y +h);
    painter->drawPath( path );

    FakePainter::DrawDashLine(painter,QPointF(x+w/3,y ) ,QPointF(x+w/3 , y + h*2/3));
    FakePainter::DrawDashLine(painter,QPointF(x+w/3 , y + h*2/3),QPointF(x+w,y + h*2/3));
    FakePainter::DrawDashLine(painter,QPointF(x+w/3 , y + h*2/3),QPointF(x,y + h ));
}
View Code

 

 

交互式画板定制/开发。 支持Net,QT开发~ 

 

你可能感兴趣的:(Qt中画弧/立体图形的类封装)