Qt 自定义PushButton
http://blog.csdn.net/zddblog/article/details/11116191
功能:鼠标弹起并在按键区域内时,按键响应。并实现normal、hover、pressed效果,PushButton大小默认为传入图片大小。
PushButton的normal、hover、pressed效果没有使用QSS实现,因为重写mouseReleaseEvent后,qss的hover效果混乱。
用法:
- loginButton = new PushButton(":/button/login_button_normal",
- ":/button/login_button_hover",
- ":/button/login_button_pressed");
loginButton = new PushButton(":/button/login_button_normal",
":/button/login_button_hover",
":/button/login_button_pressed");
或者:
- loginButton = new PushButton(":/button/login_button");
loginButton = new PushButton(":/button/login_button");
效果:
具体实现:
- #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();
- }
- }
Qt 自定义PushButton的更多相关文章
- Qt自定义插件编程小结
qt自定义组件开发步骤演示.以下所有步骤的前提是自己先编译Qtcreator源码,最好生成release版的QtCreator,否则自定义的插件嵌入QtCreator会失败!!!(这个网上教程很多) ...
- paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制
源地址:http://blog.csdn.net/attilax/article/details/12343625 paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制 效果图: ...
- QSet使用及Qt自定义类型使用QHash等算法
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSet使用及Qt自定义类型使用QHash等算法 本文地址:http://techie ...
- Qt自定义标题栏
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt自定义标题栏 本文地址:http://techieliang.com/2017/1 ...
- Qt 自定义事件(三种方法:继承QEvent,然后Send Post就都可以了,也可以覆盖customEvent函数,也可覆盖event()函数)
Qt 自定义事件很简单,同其它类库的使用很相似,都是要继承一个类进行扩展.在 Qt 中,你需要继承的类是 QEvent. 继承QEvent类,你需要提供一个QEvent::Type类型的参数,作为自定 ...
- Qt中PushButton的pressed,released,clicked三种响应的区别
Qt的PushButton的常用的三种响应有pressed,released和clicked. 优先级:pressed>released>clicked 按下按钮pressed函数的内容, ...
- Qt 自定义事件
Qt 自定义事件很简单,同其它类库的使用很相似,都是要继承一个类进行扩展.在 Qt 中,你需要继承的类是 QEvent. 继承QEvent类,你需要提供一个QEvent::Type类型的参数,作为自定 ...
- qt 中的自定义pushbutton
- Qt自定义窗口部件
QtDesigner自定义窗口部件有两种方法:改进法(promotion)和插件法(plugin) 改进法 1.改进法之前,要先写好子类化QSpinBox后的HexspinBox.h和Hexs ...
随机推荐
- [Spring Data Repositories]学习笔记--定义自己的repository
有时,我们会需要用到自己定义的一些查询方法,可以按照下面几步进行. 1. 定义一个包含该方法的接口 Interface UserRepositoryCustom { public void someC ...
- 【BZOJ2242】[SDOI2011]计算器 BSGS
[BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...
- C#动态删除控件
foreach (Control var in panel.Controls) { if (var is Billet) { panel.Controls.Remove(var); var.Dispo ...
- SSL逐渐演变到TLS
w https://zh.wikipedia.org/wiki/超文本传输安全协议
- 如何用SQL为每一行均产生一个随机数
) as int) as RndId from 表名
- 从B 树、B+ 树、B* 树谈到R 树(转)
作者:July.weedge.Frankie.编程艺术室出品. 说明:本文从B树开始谈起,然后论述B+树.B*树,最后谈到R 树.其中B树.B+树及B*树部分由weedge完成,R 树部分由Fra ...
- 脉冲神经网络及有监督学习算法Tempotron
接下来一段时间开启脉冲神经网络模型的探索之旅.脉冲神经网络有更强的生物学基础,尽可能地模拟生物神经元之间的连接和通信方式.其潜在能力较强,值得踏进一步探索. 构建脉冲神经网络模型,至少需要考虑三点:1 ...
- 神奇的Timer
最近的一个项目有一些地方需要用到定时功能,在设计过程中,突然发现.net的Timer类居然还有很多我以前没有用过的功能,这里就跟大家分享一下 注:这里的Timer类特指System.Threading ...
- java中byte数组与int,long,short间的转换
http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * <l ...
- 异常:System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)
异常:System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid) 原因:该引用所需.NET Framework版本为4.5 ...