.h文件
#ifndef VOLUMETOOLBTN_H
#define VOLUMETOOLBTN_H
#include <QToolButton>
#include <memory>
class VolumeToolBtn : public QToolButton {
Q_OBJECT
public:
explicit VolumeToolBtn(QWidget *parent = nullptr);
~VolumeToolBtn() override;
void initUi();
protected:
void enterEvent(QEnterEvent *event) override;
void leaveEvent(QEvent *event) override;
public slots:
void onClicked();
private:
bool m_isNoVolume = false;
std::unique_ptr<QWidget>m_volumeWidget;
};
#endif //VOLUMETOOLBTN_H
.cpp文件
#include "VolumeToolBtn.h"
VolumeToolBtn::VolumeToolBtn(QWidget *parent)
:QToolButton(parent)
,m_volumeWidget(std::make_unique<QWidget>(this))
{
initUi();
connect(this, &QToolButton::clicked, this, &VolumeToolBtn::onClicked);
}
VolumeToolBtn::~VolumeToolBtn() {}
void VolumeToolBtn::initUi() {
this->setStyleSheet(R"(QToolButton{border-image: url(':///Res/playbar/volume-on-gray.svg');}
QToolButton:hover{border-image: url(':///Res/playbar/volume-on-blue.svg');})");
}
。。。。。后面省略
找了半天发现原来是狗屎m_volumeWidget遮住了按钮。。。真是无语,头一回碰到这么奇葩的错误。改为m_volumeWidget(std::make_unique<QWidget>())即可
标签:控件,QToolButton,VolumeToolBtn,void,initUi,volumeWidget,VOLUMETOOLBTN,Bug,运行 From: https://blog.csdn.net/m0_74091159/article/details/142703545