本程序是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;
};
#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()));