• 32.QT绘图


    • widget.h
       1 #ifndef WIDGET_H
       2 #define WIDGET_H
       3 
       4 #include <QWidget>
       5 #include <QPainter>
       6 #include <QPen>
       7 #include <QEvent>
       8 
       9 namespace Ui {
      10 class Widget;
      11 }
      12 
      13 class Widget : public QWidget
      14 {
      15     Q_OBJECT
      16 
      17 public:
      18     explicit Widget(QWidget *parent = 0);
      19     ~Widget();
      20     void paintEvent(QPaintEvent *e);
      21     QPainter *painter;
      22 
      23 private:
      24     Ui::Widget *ui;
      25 };
      26 
      27 #endif // WIDGET_H
    • widget.cpp
       1 #include "widget.h"
       2 #include "ui_widget.h"
       3 
       4 Widget::Widget(QWidget *parent) :
       5     QWidget(parent),
       6     ui(new Ui::Widget)
       7 {
       8     ui->setupUi(this);
       9     setGeometry(120,120,600,600);
      10 }
      11 
      12 Widget::~Widget()
      13 {
      14     delete ui;
      15 }
      16 
      17 void Widget::paintEvent(QPaintEvent *e)
      18 {
      19     painter = new QPainter;
      20     painter->begin(this);
      21     painter->setPen(QPen(Qt::blue,10,Qt::DashDotLine));
      22 
      23     //画刷
      24     painter->setBrush(QBrush(Qt::red,Qt::SolidPattern));
      25     //画矩形
      26     //painter->drawRect(20,20,180,180);
      27     //画椭圆
      28     //painter->drawEllipse(20,20,190,160);
      29     //画点
      30     //painter->drawPoint(100,100);
      31     //画曲线
      32    // painter->drawArc(20,20,210,160,500,1000);
      33 
      34 
      35     /*
      36         case Line:                                             //直线
      37             p.drawLine(rect.topLeft(),rect.bottomRight());     break;
      38         case Rectangle:                                        //长方形
      39             p.drawRect(rect);    break;
      40         case RoundRect:                                        //圆角方形
      41             p.drawRoundRect(rect);     break;
      42         case Ellipse:                                          //椭圆形
      43             p.drawEllipse(rect);     break;
      44         case Polygon:                                          //多边形
      45             p.drawPolygon(points,4);     break;
      46         case Polyline:                                      //多边线
      47             p.drawPolyline(points,4);     break;
      48         case Points:                                          //点
      49             p.drawPoints(points,4);     break;
      50         case Arc:                                              //弧
      51             p.drawArc(rect,startAngle,spanAngle);     break;
      52         case Path:                                             //路径
      53             p.drawPath(path);     break;
      54         case Text:                                            //文字
      55             p.drawText(rect,Qt::AlignCenter,tr("Hello Qt!"));    break;
      56         case Pixmap:                                          //图片
      57             p.drawPixmap(150,150,QPixmap("butterfly.png"));     break;
      58         default:     break;
      59 
      60 
      61 
      62 
      63        painter->drawLine(QPoint(30,30),QPoint(60,180));
      64        painter->drawText(this->rect(),Qt::AlignCenter,tr("Hello Qthello haihua!"));
      65        painter->drawPixmap(150,150,QPixmap("1.png"));
      66     */
      67 
      68     //画线
      69     //painter->drawLine(QPoint(30,30),QPoint(60,180));
      70     //painter->drawText(this->rect(),Qt::AlignCenter,tr("Hello"));
      71     //画图
      72     painter->drawPixmap(0,0,QPixmap("1.png"));
      73     painter->end();
      74 }
  • 相关阅读:
    Java 集合框架
    Java 网络编程
    Java序列化
    Java 发送邮件
    Java 多线程编程
    Java 文档注释
    Java Applet基础
    Python 基础教程
    Python 简介
    一步步学习SPD2010--第三章节--处理列表和库(19)----关键点
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8794730.html
Copyright © 2020-2023  润新知