QT 绘制事件 PaintEvent

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


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


1. paintEvent
案例:
覆盖QWidget的paintEvent


DemoPushButton.h文件:

#ifndef DEMOPUSHBUTTON_H
#define DEMOPUSHBUTTON_H

#include <QPushButton>
#include <QPaintEvent>
#include <QMouseEvent>

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 <QColor>
#include <QBrush>
#include <QPen>
#include <QPoint>
#include <QPainter>


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值