QT4.8用Qwidget重写或者实现图片按钮+圆角算法的实现

首先博主是为了分享其次是为了锻炼自己的能力。(希望大家喜欢)

其实可以用Qwidget实现任意控件(给我一个Qwidget,还你一个任意控件)
本次图片按钮实现了,单击事件双击事件(与单击事件有冲突),长按事件
好了废话不多说。

首先时头文件(注意博主没有用ui文件):
#ifndef NIMAGEBUTTON_H
#define NIMAGEBUTTON_H 
#include 
#include 
#include 
#include 
#include 
#include "QTool.h"
#define  CLICKED_OUT_TIME 3500
/**
*图片按钮
*/
class NImageButton : public QWidget
{
	Q_OBJECT

public:
	NImageButton(QWidget *parent=0);
	~NImageButton();
	//设置图片路劲
	bool setNormalImage(QString filePath);
	bool setPressImage(QString filePath);
	//设置图片
	bool setNormalImage(QPixmap& image);
	bool setPressImage(QPixmap &image);
	//设置图片的圆角弧度
	void setImageArc(float arc);
protected:
	//重绘事件
	virtual void paintEvent(QPaintEvent *event);
	//鼠标事件
	virtual void mousePressEvent(QMouseEvent *event);
	virtual void mouseMoveEvent(QMouseEvent *event);
	virtual void mouseReleaseEvent(QMouseEvent *event);
	 virtual void mouseDoubleClickEvent(QMouseEvent *event);
private:
	void drawImageButton(QPainter &painter);
	//当前是否已发送点击事件
	bool isClickedSendEmit;
	//长按定时
	QTimer * m_pTimer;
	//按钮状态
	enum
	{
		IMAGE_BUTTON_DWON=0,IMAGE_BUTTON_UP,IMAGE_BUTTON_MOVE 
	};
	//图片状态
	int nButtonStatus;
	//圆角弧度
	float fArc;
	//正常显示图片
	QPixmap *m_normalShowImg;
	//按下显示图片
	QPixmap *m_pressShowImg;
	//信号实现
signals:
	//单击
	void clicked();
	//双击
	void doubleClicked();
	//长按
	void longClicked();
private slots:
	void onClickedOutTime();
};

#endif // NIMAGEBUTTON_H

其次时cpp文件:

#include "NImageButton.h"

NImageButton::NImageButton(QWidget *parent)
	: QWidget(parent),m_normalShowImg(NULL),m_pressShowImg(NULL),
	nButtonStatus(IMAGE_BUTTON_UP),fArc(0),m_pTimer(NULL)
{ 
	m_pTimer=new QTimer (this);
	connect(m_pTimer,SIGNAL(timeout()),this,SLOT(onClickedOutTime()));
} 
//长按超时
void NImageButton::onClickedOutTime()
{
	m_pTimer->stop();
	isClickedSendEmit = true;
	emit longClicked();
}
/**
设置普通图片路劲,图片不存在将返回false
*/
bool NImageButton::setNormalImage(QString filePath)
{
	bool isFileExits = QFile::exists(filePath);
	if (!isFileExits)
	{
		return false;
	}
	if (m_normalShowImg == NULL)
	{
		m_normalShowImg = new QPixmap();
	}
	else
	{
		if (m_normalShowImg != NULL)
		{
			delete m_normalShowImg;
			m_normalShowImg = new QPixmap();
		} 
	}
	isFileExits = m_normalShowImg->load(filePath);
	update();
	return isFileExits;
}
/**
设置按下去图片路劲,图片不存在将返回false
*/
bool NImageButton::setPressImage(QString filePath)
{
	bool isFileExits =QFile::exists(filePath);
	if (!isFileExits)
	{
		return false;
	}
	if (m_pressShowImg == NULL)
	{
		m_pressShowImg = new QPixmap();
	}
	else
	{
		if (m_pressShowImg != NULL)
		{
			delete m_pressShowImg;
			m_pressShowImg = new QPixmap();
		} 
	}
	return m_pressShowImg->load(filePath);
}
/**
设置普通图片,图片设置失败将返回false
*/
bool NImageButton::setNormalImage(QPixmap &image)
{
	if (m_normalShowImg == NULL)
	{
		m_normalShowImg = new QPixmap(image);
	}
	else
	{
		if (m_normalShowImg != NULL)
		{
			delete m_normalShowImg;
			m_normalShowImg = new QPixmap(image);
		} 
	}
	return true;
}
/**
设置按下图片,图片设置失败将返回false
*/
bool NImageButton::setPressImage(QPixmap &image)
{
	if (m_pressShowImg == NULL)
	{
		m_pressShowImg = new QPixmap(image);
	}
	else
	{
		if (m_pressShowImg != NULL)
		{
			delete m_pressShowImg;
			m_pressShowImg = new QPixmap(image);
		} 
	}
	return true;
}
//设置按钮弧度
void NImageButton::setImageArc(float arc)
{
	fArc = arc;
}
void NImageButton::mousePressEvent(QMouseEvent *event)
{
	isClickedSendEmit = false;
	m_pTimer->start(CLICKED_OUT_TIME);
	nButtonStatus = IMAGE_BUTTON_DWON;
	update();
	QWidget::mousePressEvent(event);
}
void NImageButton::mouseMoveEvent(QMouseEvent *event)
{
	nButtonStatus = IMAGE_BUTTON_MOVE;
	update();
	QWidget::mouseMoveEvent(event);
}
//双击事件
void NImageButton::mouseDoubleClickEvent(QMouseEvent *event)
{
	emit doubleClicked();
	isClickedSendEmit=true;
	QWidget::mouseDoubleClickEvent(event);
}
void NImageButton::mouseReleaseEvent(QMouseEvent *event)
{
	m_pTimer->stop();
	nButtonStatus = IMAGE_BUTTON_UP;
	update();
	//如果没有发送点击事件就发送
	if (!isClickedSendEmit)
	{
		emit clicked();
	}
	QWidget::mouseReleaseEvent(event);
}
void NImageButton::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	drawImageButton(painter);
	painter.setPen(QColor(Qt::white)); 
	QWidget::paintEvent(event);
}

NImageButton::~NImageButton()
{

}

void NImageButton::drawImageButton(QPainter &painter)
{
	QPixmap nowImage(size());
	if(nButtonStatus == IMAGE_BUTTON_UP)
	{
		if (m_normalShowImg != NULL)
		{
			nowImage =m_normalShowImg->scaled(width(),height());
		}
	}
	else if (nButtonStatus == IMAGE_BUTTON_DWON)
	{
		if (m_pressShowImg != NULL)
		{
			nowImage =m_pressShowImg->scaled(width(),height());
		}
	}
	else if(nButtonStatus == IMAGE_BUTTON_MOVE)
	{
		if (m_normalShowImg != NULL )
		{
			nowImage =m_normalShowImg->scaled(width(),height());
		}
	}
	painter.setPen(Qt::NoPen);
	painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
	nowImage=QTool::QPixmapToRound(nowImage,fArc);
	painter.drawPixmap(0,0,nowImage); 
}

如何使用:(注意图片是博主自己的你们可以自己放自己的)

NImageButton *pNImageButton = new NImageButton(this);
pNImageButton->setGeometry(10,10,120,35); 
pNImageButton->setMinimumSize(120,35);
QPixmap pp("Image/button.png");
QPixmap ppc("Image/button1.png");
pNImageButton->setNormalImage(pp);
pNImageButton->setPressImage(ppc); 
pNImageButton->setImageArc(10);

注意博主使用了圆角算法,:

//图片圆角算法
QPixmap QTool::QPixmapToRound(const QPixmap & img, int radius)
{
	if (img.isNull())
	{
		return QPixmap();
	}
	QSize size(img.size());
	QBitmap mask(size);
	QPainter painter(&mask);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.setRenderHint(QPainter::SmoothPixmapTransform);
	painter.fillRect(mask.rect(), Qt::white);
	painter.setBrush(QColor(0, 0, 0));
	painter.drawRoundedRect(mask.rect(), radius, radius);
	QPixmap image = img;// .scaled(size);
	image.setMask(mask);
	return image;
}

来张效果图:

博主的下一个重写控件预览(这周即将发布,也是重写的Qwidget)希望大家喜欢:




你可能感兴趣的:(qt,C++,技术文档)