Qt5.8 按钮三种状态:Normal、Hover、Click。支持鼠标穿透
来源:互联网 发布:室内定位技术 知乎 编辑:程序博客网 时间:2024/06/02 00:05
本程序是windows下的。
正常状态下:
Hover:
点击:
要求点击红色区域没反应,如图:
代码如下:
#pragma once#include <QWidget>#include <QPainter>#include <QBitmap>#include <QEvent>#include <QtWidgets/QPushButton>#include <QtWidgets/QToolButton>#include <inttypes.h>#include <Shlwapi.h>#pragma comment(lib, "Shlwapi.lib")enum _ButtonStatus{btnNomal = 0,btnHover = 1,btnPressed = 2};class PushButton :public QPushButton{Q_OBJECTpublic:explicit PushButton(QWidget *parent = NULL);~PushButton();void SetImage(const QString& strImage, const QString& strHoverImage, const QString& strPressedImage);private:QString SaveImagePath(const QString& strImage);private:void paintEvent(QPaintEvent *);void enterEvent(QEvent *event);void leaveEvent(QEvent *event);public slots:void onClicked();private:QString m_strImage;QString m_strHoverImage;QString m_strPressedImage;int nStart;private:float fWidthScale;float fHeightScale;};
#include "PushButton.h"#include <Windows.h>PushButton::PushButton(QWidget *parent) : QPushButton(parent){nStart = btnNomal;setStyleSheet("QPushButton{background: transparent;}");connect(this, SIGNAL(clicked()), this, SLOT(onClicked()));setMouseTracking(true);UINT32 m_ScreenWidth = GetSystemMetrics(SM_CXSCREEN);UINT32 m_ScreenHeight = GetSystemMetrics(SM_CYSCREEN);fWidthScale = (float)m_ScreenWidth / (float)1920;fHeightScale = (float)m_ScreenHeight / (float)1080;}PushButton::~PushButton(){}QString PushButton::SaveImagePath(const QString& strImage){wchar_t wcModule[1024] = {0};GetModuleFileName(NULL, wcModule, 1024);::PathRemoveFileSpec(wcModule);wsprintf(wcModule + wcslen(wcModule), L"\\%s", strImage.toStdWString().c_str());return QString::fromStdWString(wcModule);}void PushButton::SetImage(const QString& strImage, const QString& strHoverImage, const QString& strPressedImage){m_strImage = SaveImagePath(strImage);m_strHoverImage = SaveImagePath(strHoverImage);m_strPressedImage = SaveImagePath(strPressedImage);QPixmap pixmap(m_strHoverImage);QPixmap pixmap_new = pixmap.scaled(pixmap.width() * fWidthScale, pixmap.height() * fHeightScale, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);setIcon(pixmap_new);setIconSize(QSize(pixmap_new.width(), pixmap_new.height()));setMask(pixmap_new.mask());}void PushButton::leaveEvent(QEvent *event){nStart = btnNomal;}void PushButton::enterEvent(QEvent *event){nStart = btnHover;}void PushButton::onClicked(){nStart = btnPressed;}void PushButton::paintEvent(QPaintEvent *event){if (nStart == btnHover){QPixmap pixmap(m_strHoverImage);QPixmap pixmap_new = pixmap.scaled(pixmap.width() * fWidthScale, pixmap.height() * fHeightScale, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);QPainter painter(this);painter.drawPixmap(rect(), pixmap_new);setMask(pixmap_new.mask());}else if (nStart == btnPressed){QPixmap pixmap(m_strPressedImage);QPixmap pixmap_new = pixmap.scaled(pixmap.width() * fWidthScale, pixmap.height() * fHeightScale, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);QPainter painter(this);painter.drawPixmap(rect(), pixmap_new);setMask(pixmap_new.mask());}else{QPixmap pixmap(m_strImage);QPixmap pixmap_new = pixmap.scaled(pixmap.width() * fWidthScale, pixmap.height() * fHeightScale, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);QPainter painter(this);painter.drawPixmap(rect(), pixmap_new);setMask(pixmap_new.mask());}}
调用:
m_btn_ManageUser = new PushButton(this);m_btn_ManageUser->SetImage("移入效果4.png", "移入效果111.png", "移入效果2.png");m_btn_ManageUser->setGeometry(0, 0, 229, 139);connect(m_btn_ManageUser, SIGNAL(clicked()), this, SLOT(OnManageUser()));
阅读全文
0 0
- Qt5.8 按钮三种状态:Normal、Hover、Click。支持鼠标穿透
- Qt5.8 按钮跟踪鼠标状态
- Qt5.8 鼠标穿透(让鼠标点击png透明处没有反应)
- android4.0 支持监控鼠标事件hover
- 按钮hover状态的css实现
- DataGridView单元按钮列和支持三种选择状态的复选框列的实现
- Qt之实现按钮正常(Normal)、滑过(Hover)、按下(Press)效果
- OSX下原生态按钮的Hover状态
- C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
- C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
- JQuery在hover(in)状态下添加click事件的多次触发现象
- JQuery在hover(in)状态下添加click事件的多次触发现象
- 鼠标穿透
- 鼠标悬浮控制元素隐藏与显示 - css中鼠标的hover状态
- Qt5.8 按钮弹出菜单
- hover 跟着鼠标走
- jquery - hover 鼠标滑入滑出
- 鼠标hover图片改变
- easyDialog 显示弹出框
- 多年iOS开发经验总结
- adb连接出现device not found问题解决
- gvim的配色插件设置
- TensorFlow基本概念
- Qt5.8 按钮三种状态:Normal、Hover、Click。支持鼠标穿透
- 统计学习(二):统计推断
- php array 转json java 转 json数据格式
- C语言按位运算
- 操作系统励志人物
- MySQL在字段中使用select子查询
- 数字货币开发专题(X11算法矿池挖矿教程)
- new Date()
- 配置NDK环境