一个比较不错的Qt画线代码

#include "widget.h"
#include "ui_widget.h"
 
  
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    False = false;
    //connect(ui->pushButton_but,SIGNAL(clicked()),this,SLOT(Btn()));
    setFixedSize(360,360);
    setWindowTitle("hello DevDiv!");
 
  
 
  
}
void Widget::Btn()
{
 
  
 
  
}
void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPen pen;
//    //画线
      pen.setStyle(Qt::SolidLine);
     pen.setColor(Qt::blue);
      painter.setPen(pen);
      painter.drawLine(rect().topLeft(), rect().bottomRight());
 
  
    //空心矩形
    pen.setColor(Qt::darkRed);
    painter.setPen(pen);
    painter.drawRect(1,1,100,100);
 
  
   //实心矩形
    QBrush brush(Qt::CrossPattern);
    painter.setBrush(brush);
    painter.drawRect(QRect(105,1,100,100));
 
  
    //画一个多点线
    pen.setColor(Qt::blue);
    painter.setPen(pen);
    brush.setStyle(Qt::NoBrush);
    painter.setBrush(brush);
    static  const QPointF points[4] = {QPointF(210.0,1), QPointF(220.0, 50.3), QPointF(300, 100.4), QPointF(12.4, 120.0)};
    painter.drawPolyline(points,4);
 
  
    //画多个点
    QPointF pointf[10];
    for(int i=0;i<10;i++)
    {
            pointf[i].setX(2.0+i*10.0);
            pointf[i].setY(130.0);
 
  
 
  
    }
    painter.drawPoints(pointf,10);
 
  
    QLineF linef[5];
    for(int j=0;j<5;j++)
    {
        linef[j].setP1(QPointF(110.9+j*10,120.0));
        linef[j].setP2(QPointF(120.8+j*12, 200.0));
 
  
    }
     painter.drawLines(linef,5);
     QPolygonF polygon;
      polygon << QPointF(200.0, 120.0) << QPointF(230.0, 130.0) << QPointF(260.0, 180.0) << QPointF(200.0, 200.0);
      brush.setStyle(Qt::CrossPattern);
      painter.setBrush(brush);
      painter.drawPolygon(polygon, Qt::WindingFill);
 
  
      // 画一个圆角矩形
      QRectF rectangle(290.0, 110.0, 50, 50);
      brush.setStyle(Qt::NoBrush);
      painter.setBrush(brush);
      painter.drawRoundedRect(rectangle, 20.0, 15.0);
 
  
     // 画一个QString
      painter.drawText(50, 300, "Hello DevDiv!");
 
  
}
Widget::~Widget()
{
    delete ui;
}
 
 

你可能感兴趣的:(Qt)