本文主要是介绍QToolButton(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
解决QToolButton Icon没有悬浮样式的问题
#ifndef CTOOLBUTTONSTYLE_H
#define CTOOLBUTTONSTYLE_H
#include <QProxyStyle>
#include <QToolButton>class CToolButton;
class CToolButtonStyle : public QProxyStyle
{Q_OBJECTpublic: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_OBJECTQ_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;}/*去掉右下方的三角*/
这篇关于QToolButton(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!