QPushbutton实现自定义背景图,同时可以使用样式表设置不同的背景色

QPushbutton实现自定义背景图,同时使用样式表设置不同的背景色

1.新建一个ui文件,里面防止一个QPushbutton

UI文件

2.头文件

#pragma once

#include <QWidget>
#include "ui_DeletePushButtonWidget.h"
#include <QLabel>
#include <QVBoxLayout>
class DeletePushButtonWidget : public QWidget
{
	Q_OBJECT

public:
	DeletePushButtonWidget(QWidget *parent = Q_NULLPTR);
	~DeletePushButtonWidget();

private:
	Ui::DeletePushButtonWidget ui;
public:
	bool isButtonChecked();
	void setCheckedState(bool isChecked);
public:
	QLabel *m_label_img;
	QVBoxLayout* m_VBoxLayout;
signals:
	void sig_Clicked(bool isCheck = false);
private:
	private slots:
	void slt_pbtn_Clicked(bool isCheck);
};

3 源文件

#include "DeletePushButtonWidget.h"
#include <QPixmap>
#include <QBitmap>
DeletePushButtonWidget::DeletePushButtonWidget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	ui.pushButton->setToolTip(QObject::tr("delete"));
	ui.pushButton->setText("");

	connect(ui.pushButton, &QPushButton::clicked, this, &DeletePushButtonWidget::slt_pbtn_Clicked);
	ui.pushButton->setStyleSheet("QPushButton{border: 0px solid #B7B9BE;  border-image: none;    border-radius: 4px;}\
								 QPushButton:hover{border-image: none;    background-color: rgba(146,229,215,30%);}\
								 QPushButton:checked{border-image: none;  background-color: rgba(146,229,215,80%);}");
	/*ui.pushButton->setStyleSheet("QPushButton{border: 0px solid #B7B9BE;  border-image: none;    border-radius: 4px;}\
								 QPushButton:hover{border-image: none;    border: 4px dotted rgba(146,229,215,30%);}\
								 QPushButton:checked{border-image: none;  background: rgba(146,229,215,80%);	border: 4px solid rgba(146,229,215,80%);}");
								 */
	m_label_img = new QLabel(ui.pushButton);
	m_label_img->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
	QPixmap showImg(":/new/prefix1/Resources/delete.png");
	showImg = showImg.scaled(26, 26, Qt::KeepAspectRatio, Qt::FastTransformation);
	//ui.label_img->setScaledContents(true);
	m_label_img->setPixmap(showImg);
	m_label_img->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	m_VBoxLayout = new QVBoxLayout(ui.pushButton);
	m_VBoxLayout->setContentsMargins(6, 6, 6, 6);
	m_VBoxLayout->addWidget(m_label_img);
	ui.pushButton->setLayout(m_VBoxLayout);

	
	//“QPushButton{ background - color:#FF0000 }; ” //常态
	//“QPushButton : hover{ background - color:#00FF00 }; ” //聚焦状态
	//“QPushButton : pressed{ background - color:#0000FF }; ” //按下状态
	//“QPushButton : disabled{ background - color:#DDDDDD }; ” //禁用状态

}

DeletePushButtonWidget::~DeletePushButtonWidget()
{
	if (m_label_img != nullptr)
	{
		delete m_label_img;
		m_label_img = nullptr;
	}
	if (m_VBoxLayout != nullptr)
	{
		delete m_VBoxLayout;
		m_VBoxLayout = nullptr;
	}
}
bool DeletePushButtonWidget::isButtonChecked()
{
	return ui.pushButton->isChecked();
}
void DeletePushButtonWidget::setCheckedState(bool isChecked)
{
	ui.pushButton->setChecked(isChecked);

}
void DeletePushButtonWidget::slt_pbtn_Clicked(bool isCheck)
{
	emit sig_Clicked(isCheck);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值