国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

QT4.8用Qwidget重寫或者實現圖片按鈕+圓角算法的實現

2019-11-06 06:23:36
字體:
來源:轉載
供稿:網友
首先博主是為了分享其次是為了鍛煉自己的能力。(希望大家喜歡)其實可以用Qwidget實現任意控件(給我一個Qwidget,還你一個任意控件)本次圖片按鈕實現了,單擊事件,雙擊事件(與單擊事件有沖突),長按事件好了廢話不多說。首先時頭文件(注意博主沒有用ui文件):
#ifndef NIMAGEBUTTON_H#define NIMAGEBUTTON_H #include <QWidget>#include <QPixmap>#include <QPainter>#include <QTimer>#include <QMouseEvent>#include "QTool.h"#define  CLICKED_OUT_TIME 3500/***圖片按鈕*/class NImageButton : public QWidget{	Q_OBJECTpublic:	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)希望大家喜歡:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿勒泰市| 远安县| 高邮市| 云梦县| 荃湾区| 固原市| 工布江达县| 长兴县| 镇巴县| 嵊泗县| 武汉市| 巴楚县| 龙南县| 澎湖县| 牡丹江市| 阿拉善右旗| 乐昌市| 清原| 岳池县| 拉萨市| 两当县| 广宗县| 喀喇| 南澳县| 嘉荫县| 深州市| 浮梁县| 香河县| 临沧市| 盐城市| 珲春市| 两当县| 讷河市| 泸州市| 句容市| 蚌埠市| 汾西县| 天镇县| 聂拉木县| 民勤县| 柳州市|