QPushButton改变形状

改变QPushButton形状

#ifndef CIMAGEBUTTON_H
#define CIMAGEBUTTON_H

#include <QPushButton>

class CImageButton : public QPushButton
{
	Q_OBJECT

public:
	CImageButton(QWidget *parent);
	~CImageButton();
	void SetPixmap(QPixmap* pNormal,QPixmap* pHover,QPixmap* pLighted);
	void SetLighted(bool value);
public:
	void paintEvent(QPaintEvent* pEvent);
	void mousePressEvent(QMouseEvent *e);
	void mouseReleaseEvent(QMouseEvent *e);
	void enterEvent(QEvent *e);
	void leaveEvent(QEvent *e);
protected:
	bool m_bLighted;
	bool m_bMouseIN;
	bool m_bLeftDown;
	QPixmap* m_pHover;
	QPixmap* m_pNormal;
	QPixmap* m_pLighted;
	
};

#endif // CIMAGEBUTTON_H
#include "cimagebutton.h"
#include <QtGui>
CImageButton::CImageButton(QWidget* pParent):QPushButton(pParent),
	m_bMouseIN(false),
	m_bLeftDown(false),
	m_bLighted(false)
{
}
CImageButton::~CImageButton()
{

}
void CImageButton::SetPixmap(QPixmap* pNormal,QPixmap* pHover,QPixmap* pLighted)
{
	m_pNormal=pNormal;
	m_pHover=pHover;
	m_pLighted=pLighted;
}

void CImageButton::SetLighted(bool value)
{
	if(value != m_bLighted)
	{
		m_bLighted=value;
		repaint();
	}
}

void CImageButton::paintEvent(QPaintEvent* pEvent)
{
	QPixmap* pPixmap;
	if(m_bLighted || m_bLeftDown)
		pPixmap=m_pLighted;
	else
	{
		if(m_bMouseIN)
			pPixmap=m_pHover;
		else
			pPixmap=m_pNormal;
	}
	QPainter painter;
	painter.begin(this);
	if(pPixmap)
		painter.drawPixmap(rect(),*pPixmap);
	if(text().count() > 0)
		painter.drawText(rect(),Qt::AlignCenter,text());
	painter.end();
}

void CImageButton::mousePressEvent(QMouseEvent *e)
{
	if(e->button()==Qt::LeftButton)
	{
		m_bLeftDown=true;
		repaint();
	}
	QPushButton::mousePressEvent(e);
}

void CImageButton::mouseReleaseEvent(QMouseEvent *e)
{
	if(e->button()==Qt::LeftButton)
	{
		m_bLeftDown=false;
		repaint();
	}
	QPushButton::mouseReleaseEvent(e);
}

void CImageButton::enterEvent(QEvent *e)
{
	m_bMouseIN=true;
	repaint();
}

void CImageButton::leaveEvent(QEvent *e)
{
	m_bMouseIN=false;
	repaint();
}
//应用,这样QPushButton形状就是图片的形状
	CImageButton* m_pOrderAbove=new CImageButton(this);
	m_pOrderAbove->SetPixmap(pPixOrderAbove,pPixOrderAbove,pPixOrderAbove);
	m_pOrderAbove->setText(QString::fromLocal8Bit("天亮了"));
	m_pOrderAbove->setStyleSheet("color: rgb(255, 255, 255);");
	m_pOrderAbove->setGeometry(144,0,pPixOrderAbove->width(),pPixOrderAbove->height());




  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

致一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值