QToolButton(2)
2023-12-14 16:43:44
?解决QToolButton Icon没有悬浮样式的问题
#ifndef CTOOLBUTTONSTYLE_H
#define CTOOLBUTTONSTYLE_H
#include <QProxyStyle>
#include <QToolButton>
class CToolButton;
class CToolButtonStyle : public QProxyStyle
{
Q_OBJECT
public:
CToolButtonStyle(QToolButton* parent);
virtual void drawItemPixmap(QPainter* painter, const QRect& rect, int, const QPixmap& pixmap) const override;
virtual void drawItemText(QPainter* painter, const QRect& rect, int flags, const QPalette& pal, bool enabled,
const QString& text, QPalette::ColorRole textRole = QPalette::NoRole) const override;
private:
CToolButton* m_pButton;
};
#endif
#include "CButtonStyle.h"
#include "CToolButton.h"
#include <QPixmap>
CToolButtonStyle::CToolButtonStyle(QToolButton* parent)
: QProxyStyle()
{
setParent(parent);
m_pButton = dynamic_cast<CToolButton*>(parent);
}
void CToolButtonStyle::drawItemPixmap(QPainter* painter, const QRect& rect, int, const QPixmap& pixmap) const
{
QPixmap tmpPixmap;
if(m_pButton->curStatus()==CToolButton::ButtonStatus::NORMAL)
{
tmpPixmap.load(m_pButton->getNormalIconPath());
}
else if(m_pButton->curStatus()==CToolButton::ButtonStatus::HOVER)
{
tmpPixmap.load(m_pButton->getHoverIconPath());
}
QProxyStyle::drawItemPixmap(painter, rect, Qt::AlignCenter, tmpPixmap);
}
void CToolButtonStyle::drawItemText(QPainter* painter, const QRect& rect, int flags, const QPalette& pal,
bool enabled, const QString& text, QPalette::ColorRole textRole) const
{
QProxyStyle::drawItemText(painter, rect, flags | Qt::AlignCenter, pal, enabled, text, textRole);
}
#ifndef CTOOLBUTTON_H
#define CTOOLBUTTON_H
#include <QToolButton>
class CToolButtonStyle;
class CToolButton : public QToolButton
{
Q_OBJECT
Q_PROPERTY(QString normalIconPath READ getNormalIconPath WRITE setNormalIconPath)
Q_PROPERTY(QString hoverIconPath READ getHoverIconPath WRITE setHoverIconPath)
public:
enum ButtonStatus
{
NORMAL = 0,
HOVER
};
public:
CToolButton(QWidget* parent = nullptr);
CToolButton(const QString& text, QWidget* parent = nullptr);
ButtonStatus curStatus()const{return m_btnStatus;}
QString getNormalIconPath();
void setNormalIconPath(const QString& strIconPath);
QString getHoverIconPath();
void setHoverIconPath(const QString& strIconPath);
protected:
virtual void leaveEvent(QEvent* event) override;
virtual void enterEvent(QEvent* event) override;
private:
ButtonStatus m_btnStatus;
QPixmap m_pixmap;
QString m_normalImagePath;
QString m_hoverImagePath;
CToolButtonStyle* m_pToolBtnStyle;
};
#endif
#include "CToolButton.h"
#include <QEvent>
#include "CButtonStyle.h"
CToolButton::CToolButton(QWidget* parent)
: CToolButton("", parent)
{
}
CToolButton::CToolButton(const QString& text, QWidget* parent)
: QToolButton(parent)
, m_btnStatus(ButtonStatus::NORMAL)
, m_pToolBtnStyle(nullptr)
{
setText(text);
m_pToolBtnStyle = new CToolButtonStyle(this);
setStyle(m_pToolBtnStyle);
}
QString CToolButton::getNormalIconPath()
{
return m_normalImagePath;
}
void CToolButton::setNormalIconPath(const QString& strIconPath)
{
m_normalImagePath = strIconPath;
}
QString CToolButton::getHoverIconPath()
{
return m_hoverImagePath;
}
void CToolButton::setHoverIconPath(const QString &strIconPath)
{
m_hoverImagePath=strIconPath;
}
void CToolButton::leaveEvent(QEvent* event)
{
m_btnStatus = ButtonStatus::NORMAL;
QToolButton::leaveEvent(event);
}
void CToolButton::enterEvent(QEvent* event)
{
m_btnStatus = ButtonStatus::HOVER;
QToolButton::enterEvent(event);
}
?
?使用方法:
CToolButton *button=new CToolButton(this);
button->setObjectName("MyButton");
button->setGeometry(100,200,70,32);
button->setPopupMode(QToolButton::InstantPopup);
button->setMenu(menu1);
QToolButton#MyButton
{
qproperty-icon: url(:/file_pressed.png) center;
qproperty-normalIconPath: url(:/file_hover.png);
qproperty-hoverIconPath: url(:/file_pressed.png);
qproperty-iconSize: 18px 18px;
qproperty-toolButtonStyle:ToolButtonTextBesideIcon;
background-color:transparent;
qproperty-text:"文件";
border:none;
}
QToolButton#MyButton:hover
{
color:blue;
}
QToolButton#MyButton:pressed
{
color:red;
}
QToolButton::menu-indicator{width:0px;}/*去掉右下方的三角*/
?
文章来源:https://blog.csdn.net/xiaoyao_zhang/article/details/134996611
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!