Qt 自定义PushButton
来源:互联网 发布:90后创业开淘宝店 编辑:程序博客网 时间:2024/05/21 07:09
功能:鼠标弹起并在按键区域内时,按键响应。并实现normal、hover、pressed效果,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 2class QPixmap;class PushButton : public QPushButton{ //Q_OBJECTpublic: 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
0 0
- Qt 自定义PushButton
- Qt 自定义PushButton
- Qt 自定义PushButton
- Qt 自定义PushButton
- QT Pushbutton 美化
- [Qt练习]pushButton 外观
- 设计Qt pushbutton颜色
- QT Pushbutton 美化
- qt pushbutton美化
- Qt C++自制PushButton
- QT 使用pushbutton(2)
- 窗体部件之自定义PushButton
- QT学习笔记----Pushbutton,label控件应用
- pushButton被hide之后。。(QT编程)
- Qt 在控件上面绘图 label,pushbutton。。。。。
- Qt 在控件上面绘图 label,pushbutton。。。。。
- QT 取消ui->PushButton的边框
- minimalcomps pushbutton
- Java设计模式之封装算法的模板模式
- Git 管理分支
- XX-net部署技巧
- http://www.race604.com/android-context-intro-2/
- 理想就是办一所真正的民办高校
- Qt 自定义PushButton
- 第三周—项目4 顺序表应用
- 【索引】2015 ACM/ICPC Asia Regional Online
- Linux(centos)系统各个目录的作用详解
- 面向对象的需求分析
- UIWebView不能连接网络,并且报错App Transport Security has blocked a
- Redis教程(四):java中使用redis
- SSH三大框架搭建(非常之基础)
- ubunt14.04.3的vnc中,ti ccsv5 java异常退出,导致ccsv5崩溃