QT开发应用程序(11)--图形绘制和文本输出

代码演示:

#include <QPainter>
MyDraw::MyDraw(QWidget *parent) :
    QWidget(parent)
{
    QLinearGradient linearGradient(0,0,400,400);
    linearGradient.setColorAt(0.0,Qt::white);
    linearGradient.setColorAt(0.2,QColor(0,0,0));
    linearGradient.setColorAt(1.0,Qt::black);
    linearGradient.setSpread(Qt::PadSpread);
    brush =   linearGradient;     
    pen = QPen(QColor(0,0,0),1,Qt::SolidLine,Qt::FlatCap,Qt::MiterJoin);
    setPalette(QPalette(Qt::white));
    setAutoFillBackground(true);
    setMinimumSize(400,400);
}
void MyDraw::paintEvent(QPaintEvent *)
{
	 QPainter p(this);
    p.setPen(pen);
    p.setBrush(brush);
      QRect rect(50,100,300,200);

    static const QPoint points[4]=
    {
        QPoint(150,100),
        QPoint(300,150),
        QPoint(350,250),
        QPoint(100,300)
    };
    int startAngle =30*16;
    int spanAngle =120*16;

    QPainterPath path;
    path.addRect(150,150,100,100);
    path.moveTo(100,100);
    path.cubicTo(300,100,200,200,300,300);
    path.cubicTo(100,300,200,200,100,100);
    path.setFillRule(fillRule);
	p.drawLine(rect.topLeft(),rect.bottomRight());//直线
    p.drawRect(rect);	//长方形
    p.drawRoundRect(rect);//圆角方形
    p.drawEllipse(rect); 	//椭圆形
    p.drawPolygon(points,4);  //多边形
    p.drawPolyline(points,4);//多边线
    p.drawPoints(points,4);//点
    p.drawArc(rect,startAngle,spanAngle);//弧
    p.drawPath(path);	//路径
    p.drawText(rect,Qt::AlignCenter,tr("Hello Qt!"));  //文字
     p.drawPixmap(150,150,QPixmap("butterfly.png"));//图片
}

运行效果演示:

在这里插入图片描述

发布了30 篇原创文章 · 获赞 9 · 访问量 930

猜你喜欢

转载自blog.csdn.net/x879014419/article/details/105141194