Qt5.8 按钮三种状态:Normal、Hover、Click。支持鼠标穿透

本程序是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_OBJECT

public:
	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;
};



.cpp:

#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()));



  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值