Qt 自定义PushButton
2015-10-02 21:39
405 查看
功能:鼠标弹起并在按键区域内时,按键响应。并实现normal、hover、pressed效果,PushButton大小默认为传入图片大小。
源码:cpp
.h文件
效果:
eg:
以上 留存...
出处:Qt 自定义PushButton
源码:cpp
#include "pushbutton.h" #include <QPainter> #include <QMouseEvent> #include <QFontMetrics> #include <QLabel> PushButton::PushButton(QString normal, QString hover, QString pressed, QWidget *parent) : QPushButton(parent) { buttonState = Normal; normalPixmap.load(normal); hoverPixmap.load(hover); pressPixmap.load(pressed); this->setFixedSize(normalPixmap.size()); this->setContentsMargins(0, 0, 0, 0); } PushButton::PushButton(QString background, QWidget *parent) : QPushButton(parent) { buttonState = Normal; normalPixmap.load(background); hoverPixmap.load(background); pressPixmap.load(background); this->setFixedSize(normalPixmap.size()); this->setContentsMargins(0, 0, 0, 0); } PushButton::~PushButton() { } void PushButton::paintEvent(QPaintEvent *) { QPainter painter(this); switch(buttonState) { case Normal: painter.drawPixmap(this->rect(), normalPixmap); break; case Hover: painter.drawPixmap(this->rect(), hoverPixmap); break; case Pressed: painter.drawPixmap(this->rect(), pressPixmap); } painter.drawText(this->rect(), Qt::AlignCenter, this->text()); } void PushButton::enterEvent(QEvent *) { buttonState = Hover; update(); } void PushButton::leaveEvent(QEvent *) { buttonState = Normal; update(); } void PushButton::mousePressEvent(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { buttonState = Pressed; update(); } } bool PushButton::isOnPushButton(const QPoint &point, const PushButton *pushButton) { if(point.x() < 0 || point.x() > pushButton->width() || point.y() < 0 || point.y() > pushButton->height()) { return false; } return true; } void PushButton::mouseReleaseEvent(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { //判断鼠标抬起时是否在PushButton之上 if(isOnPushButton(e->pos(), this)) { emit clicked(); } buttonState = Hover; update(); } }
.h文件
#ifndef PUSHBUTTON_H #define PUSHBUTTON_H #include <QPushButton> #define Normal 0 #define Hover 1 #define Pressed 2 class QPixmap; class PushButton : public QPushButton { //Q_OBJECT public: explicit PushButton(QString normal, QString hover, QString pressed, QWidget *parent=0); explicit PushButton(QString background, QWidget *parent=0); ~PushButton(); bool isOnPushButton(const QPoint &point, const PushButton *pushButton); protected: void paintEvent(QPaintEvent *); void enterEvent(QEvent *); void leaveEvent(QEvent *); void mousePressEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); //void loadPixmap(QString path); private: char buttonState; QPixmap normalPixmap; QPixmap hoverPixmap; QPixmap pressPixmap; }; #endif // PUSHBUTTON_H
效果:
eg:
PushButton *xxx = new PushButton(":/image/contact.png",":/image/cut-the-rope.png",":/image/drop.png",this);
以上 留存...
出处:Qt 自定义PushButton
相关文章推荐
- QT5 学习之路09---event()
- QT5 学习之路08---事件的接受与忽略
- QT5 学习之路07---QT5事件
- OK6410与GPRS连接实现与PC机通信,Qt移植
- 静态编译Qt5.4.1和Qt WebKit
- OSX下安装PyQt
- ZetCode PyQt4 tutorial basic painting
- VC生成的DLL给QT的EXE调用时lib路径问题小结
- WIN7 64位+VS2013+Qt5.5.0+opencv2.4.10配置
- ZetCode PyQt4 tutorial custom widget
- ZetCode PyQt4 tutorial Drag and Drop
- ZetCode PyQt4 tutorial widgets II
- ZetCode PyQt4 tutorial widgets I
- 使用clion和Qt5写程序时的链接问题
- VNC Viewer中启动qt5.5.0的qtcreator,报告QXcbConnection: Failed to initialize XRandr等,无法启动
- ZetCode PyQt4 tutorial Dialogs
- QT 布局管理器的嵌套使用
- ZetCode PyQt4 tutorial signals and slots
- QT中添加的资源文件qrc时的路径问题小结
- Ubuntu下Qt-4.7.1的静态编译