QT 绘制事件 PaintEvent

阅读更多
对事件的响应是通过信号与槽调用
     事件中根据事件触发的条件发出不同的信号
     比如:鼠标事件发出clicked与dblclicked等信号


QT中事件是怎么实现的?
  QT把事件自己定义成固定的virtual保护函数
  这些函数自动被系统调用


1.      paintEvent
案例:
覆盖QWidget的paintEvent


DemoPushButton.h文件:

#ifndef DEMOPUSHBUTTON_H
#define DEMOPUSHBUTTON_H

#include
#include
#include

class DemoPushButton : public QPushButton
{
public:
    DemoPushButton(QWidget * parent=NULL);

protected:
    virtual void paintEvent(QPaintEvent * e);
    virtual void enterEvent(QEvent * e);
    virtual void leaveEvent(QEvent * e);
    virtual void mousePressEvent(QMouseEvent*e);

private:
    bool  israised;

};

#endif // DEMOPUSHBUTTON_H



DemoPushButton.cpp文件:

#include "DemoPushButton.h"

#include
#include
#include
#include
#include


DemoPushButton::DemoPushButton(QWidget*parent)
    :QPushButton(parent),israised(true)
{
    setMouseTracking(true);   //跟踪鼠标移动事件
}

void DemoPushButton::paintEvent(QPaintEvent* e)
{
    QPushButton::paintEvent(e);// return;
    //绘制按钮的边界
    int  w=width();   //按钮宽度
    int  h=height();   //按钮高度
    QColor clrw(255,255,255);
    QColor clrb(0,0,0);
    QBrush brw(clrw);
    QBrush brb(clrb);
    QPen  penw(brw,2);
    QPen  penb(brb,2);
    QPoint  pttop(w/2,0);
    QPoint ptbottom(w/2,h);
    QPoint ptleft(0,h/2);
    QPoint ptright(w,h/2);
    QPainter  g(this);
    if(israised)
    {
        g.setPen(penw);
    }
    else
    {
        g.setPen(penb);
    }

    g.drawLine(pttop,ptleft);
    g.drawLine(pttop,ptright);

    if(israised)
    {
        g.setPen(penb);
    }
    else
    {
        g.setPen(penw);
    }

    g.drawLine(ptbottom,ptleft);
    g.drawLine(ptbottom,ptright);

    QPen penc;
    penc.setStyle(Qt::DashDotLine);
    penc.setWidth(3);
    penc.setBrush(Qt::gray);
    penc.setCapStyle(Qt::RoundCap);
    penc.setJoinStyle(Qt::RoundJoin);
    g.setPen(penc);
    g.drawRect(0, 0, width(), height()/2 );

    //QPushButton::paintEvent(e);
}


void DemoPushButton::enterEvent(QEvent * e)
{
    israised=false;
    repaint();     //重新绘制按钮
}


void DemoPushButton::leaveEvent(QEvent * e)
{
    israised=true;
    repaint();
}


void DemoPushButton::mousePressEvent(QMouseEvent * e)
{
    //repaint();

    QPushButton::mousePressEvent(e);
}






QPainter类:
              函数分成:
                            属性函数
                                          背景
                                          刷子
                                          遮罩
                                          笔
                                          字体
                            绘制函数
                                          图形
                                                        空心draw****
                                                        实心fill****
                                          图像
                                          字符串         
                            变换函数
                                          translate
                                          scale
                                          rotate  旋转
                                          shear

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