#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)希望大家喜歡:

新聞熱點
疑難解答