QT 绘制事件 PaintEvent

 对事件的响应是通过信号与槽调用

     事件中根据事件触发的条件发出不同的信号

     比如:鼠标事件发出clicked与dblclicked等信号

    

QT中事件是怎么实现的?

  QT把事件自己定义成固定的virtual保护函数

  这些函数自动被系统调用

 

1.      paintEvent

案例:

1.      覆盖QWidget的paintEvent

绘制gdi(Graphics DrawInterface)

代码:

main.cpp

#include<QApplication>

#include<QWidget>

#include “PaintWidget.h”

int  main(int  args , char  **argv)

{

    QApplication  app(args , argv);

    PaintWidget w;

    w.resize(400,400);

    w.setVisible(true);

     return  app.exec();

}

 

main.pro

TEMPLATE=app

SOURCES=main.cpp PaintWidget.cpp

HEADERS=PaintWidget.h

CONFIG=release qt

QT=core gui

TARGET=main

 

 

PaintWidget.h

#ifndef PAINT_WIDGET_H

#define PAINT_WIDGET_H

#include<QWidget>

#include<QPaintEvent>

class PaintWidget:public QWidget

{

protected:

      virtual void paintEvent( QPaintEvent * e);

};

#endif

 

PaintWidget.cpp

 

#include “PaintWidget.h”

#include<QPainter>

#include<QPoint>

#include<QImage>

#include<QPixmap>

#include<QBitmap>

#include<QPen>

 

void PaintWidget::paintEvent(QPaintEvent * e)

{

     QPoint p[3];

     p[0].setX(10);

     p[0].setY(10);

     p[1].setX(300);

     p[1].setY(10);

     p[2].setX(145);

     p[2].setY(200);

     QImage img(“ok.png”);

     QPixmap  map(“ok.png”);

     QBitmap  bit=map.createMaskFromColor(QColor(0,0,0));

//     QPen pen(QColor(255,0,0));

     QBrush br(QColor(0,255,0));

      QPen pen();

     

      //纯黑色设置为透明

     //进行绘制工作

     //1.产生绘制对象QPainter

     QPainter  g(this);  //this参数指定绘制的目标

     //2.设置绘制对象的属性

     g.setPen(pen);

     g.setFont(“楷书”,45,100);   //设置字体

     //3.进行绘制:图形,图像与字符串

     g.drawLine(10,10,400,400);

     g.drawArc(10,10,300,300,10*16,120*16);

     //前4个参数为弧的外切矩形,参数5,6为起始角度和结束角度,1/16度数

     g.drawConvexPolygon(p,3);  //参数2为多边形的顶点个数

     g.drawEllipase(0,0,this->width(),this->height());

     g.drawText(100,100, “this is a maomaochong”);

     g.drawImage(10,10,img);

      g.drawPixmap(10,10,300,300,bit); 

}

 

QPainter类:

              函数分成:

                            属性函数

                                          背景

                                          刷子

                                          遮罩

                                          笔

                                          字体

                            绘制函数

                                          图形

                                                        空心draw****

                                                        实心fill****

                                          图像

                                          字符串          

                            变换函数

                                          translate

                                          scale

                                          rotate  旋转

                                          shear

你可能感兴趣的:(qt,绘制事件,PaintEvent)