窗体部件之自定义PushButton
来源:互联网 发布:直销软件开发xajczxrj 编辑:程序博客网 时间:2024/06/10 02:43
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);}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 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(); }}
另外一种用图片组分割的方法设置样式表
QPushButton
{
border-image: url(:/images/9d.png) 0 354 0 0;
}
QPushButton:hover
{
border-image:url(:/images/9d.png) 0 236 0 118;
}
QPushButton:pressed
{
border-image:url(:/images/9d.png) 0 118 0 236;
}
阅读全文
0 0
- 窗体部件之自定义PushButton
- 窗体部件之Buddy
- 窗体部件之Dialogs
- 窗体部件之QActionGroup
- 窗体部件之QButtonGroup
- 窗体部件之QCompleter
- 窗体部件之QDesktopWidget
- 窗体部件之QDockWidget
- 窗体部件之QMdiArea
- 窗体部件之QWizard
- 窗体部件之QSizeGrip
- 窗体部件之QSystemTrayIcon
- 窗体部件之QToolBar
- 窗体部件之QToolBox
- 窗体部件之QCheckBox
- 窗体部件之QComboBox
- 窗体部件之QMenu
- 窗体部件之QTabWidget
- 【BFS】hdu 2717 Catch That Cow
- PHP判断是不是爬虫的方法
- Apache集群
- final,finalize,finally的区别
- 【面经笔记】 MFC消息机制
- 窗体部件之自定义PushButton
- 工厂方法模式
- if条件判断语句
- js 获取select标签的选中值
- 浅析面对对象 六大原则(五)
- java string类型xml解析为对象
- gdb调试
- 杭电2030汉字统计
- 【接口】-请求WebApi的2种方式