Qt4.8.2 QPushButton按钮贴图以及实现按钮的几种常用状态
1个回答
展开全部
一、继承自QPushButton,不改变图片本身,而是通过改变按钮背景的透明度myPushButton.h#ifndef MYPUSHBUTTON_H #define MYPUSHBUTTON_H #include <QPushButton> class myPushButton : public QPushButton { Q_OBJECT public: explicit myPushButton(QWidget *parent = 0); explicit myPushButton(QString iconStr,QWidget *parent=0); signals: public slots: private: void setBkPalette(int transparency);//设置透明度 protected: void enterEvent(QEvent *);//鼠标进入事件 void leaveEvent(QEvent *);//离开 void mousePressEvent(QMouseEvent *e);//点击 void mouseReleaseEvent(QMouseEvent *e);//释放 }; #endif // MYPUSHBUTTON_H myPushButton.cpp
#include "mypushbutton.h" #include <QPalette> #include <QPixmap> myPushButton::myPushButton(QWidget *parent) : QPushButton(parent) { } myPushButton::myPushButton(QString iconStr, QWidget *parent):QPushButton(parent) { QPixmap pixmap(iconStr); setIcon(QIcon(pixmap)); setIconSize(pixmap.size()); setBkPalette(0);//设置背景完全透明 setFlat(true); setAutoFillBackground(true); } void myPushButton::setBkPalette(int transparency)//设置背景透明度 { QPalette palette; palette.setBrush(QPalette::Button,QBrush(QColor(255,255,255,transparency))); setPalette(palette); } void myPushButton::enterEvent(QEvent *) { setBkPalette(120); } void myPushButton::leaveEvent(QEvent *) { setBkPalette(0); } void myPushButton::mousePressEvent(QMouseEvent *e) { setBkPalette(150); } void myPushButton::mouseReleaseEvent(QMouseEvent *e) { setBkPalette(120); emit clicked();//必须写上,否则不会发出clicked信号 } 二、背景一直为透明色,通过鼠标事件改变按钮的贴图,需要用到事件过滤器widget.h#ifndef WIDGET_H #define WIDGET_H #include <QtGui/QWidget> class myPushButton; class QPushButton; class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: //枚举按钮的几种状态 enum btnStatus{btnNormal,btnEnter,btnClick,btnNoStatus}; myPushButton *btn1;//通过改变背景透明度 QPushButton *btnClose;//通过改变按钮图片 btnStatus btnCloseStatus; void drawClose();//给btnClose贴图 protected: bool eventFilter(QObject *, QEvent *);//事件过滤器 }; #endif // WIDGET_H widget.cpp
#include "widget.h" #include "mypushbutton.h" #include <QVBoxLayout> #include <QPalette> #include <QPushButton> #include <QEvent> Widget::Widget(QWidget *parent) : QWidget(parent) { QPalette palette; palette.setBrush(QPalette::Window,QBrush(QColor(202,189,52))); setPalette(palette); btn1=new myPushButton(":/img/btnwifi.png"); btnClose=new QPushButton; btnClose->setStyleSheet("background:transparent;border:0px;"); btnCloseStatus=btnNoStatus; drawClose(); //安装事件过滤器,处理鼠标的进入,离开,按下,释放的事件 btnClose->installEventFilter(this); QVBoxLayout *mainLayout=new QVBoxLayout; mainLayout->addWidget(btn1); mainLayout->addWidget(btnClose); setLayout(mainLayout); } Widget::~Widget() { } void Widget::drawClose() { static QPixmap pixmap(":/img/btn_close.png"); static int width=pixmap.width()/4; static int height=pixmap.height(); btnClose->setIcon(QIcon(pixmap.copy(btnCloseStatus*width,0,width,height))); btnClose->setIconSize(QSize(width,height)); } bool Widget::eventFilter(QObject *obj, QEvent *event) { if(obj==btnClose) { btnCloseStatus=btnNoStatus; if(event->type()==QEvent::Enter) btnCloseStatus=btnEnter; else if(event->type()==QEvent::Leave) btnCloseStatus=btnNormal; else if(event->type()==QEvent::MouseButtonPress) btnCloseStatus=btnClick; else if(event->type()==QEvent::MouseButtonRelease) btnCloseStatus=btnEnter; if(btnCloseStatus!=btnNoStatus) drawClose(); } return QWidget::eventFilter(obj,event); }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询