您好,登錄后才能下訂單哦!
三態按鈕(普通態,鼠標停留態,點擊態)在設計炫麗界時是很常用的一項功能,但QT并沒有直接支持這個功能,只好自己寫一個了,通過繼承QToolButton實現。
上代碼:
[cpp] view plain copy print?
#include <QtGui>
class CoolButton : public QToolButton
{
Q_OBJECT
private:
QIcon m_NormalIcon;
QIcon m_FocusIcon;
QIcon m_PressedIcon;
protected:
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
public:
CoolButton(QWidget *parent = 0);
virtual ~CoolButton();
void SetButtonIcons(const QIcon &normal,
const QIcon &focus = QIcon(),
const QIcon &pressed = QIcon() );
};
CoolButton::CoolButton(QWidget *parent)
: QToolButton(parent)
{
//為了讓圖標看起來自然一點而設置了這兩個操作
setDown(false);
setFocusPolicy(Qt::NoFocus);
}
CoolButton::~CoolButton()
{
}
//重載“進入”和“離開”事件,進入時切換為另一個圖標,離開時還原
void CoolButton::enterEvent(QEvent *event)
{
if( isEnabled() && !m_FocusIcon.isNull() )
setIcon(m_FocusIcon);
}
void CoolButton::leaveEvent(QEvent *event)
{
if( isEnabled() )
setIcon(m_NormalIcon);
}
//重載鼠標事件
void CoolButton::mousePressEvent(QMouseEvent *event)
{
if( isEnabled() && !m_PressedIcon.isNull() )
setIcon(m_PressedIcon);
QToolButton::mousePressEvent(event);
}
void CoolButton::mouseReleaseEvent(QMouseEvent *event)
{
if( isEnabled() && m_FocusIcon.isNull() )
setIcon(m_FocusIcon);
QToolButton::mouseReleaseEvent(event);
}
void CoolButton::SetButtonIcons(const QIcon &normal,
const QIcon &focus, const QIcon &pressed)
{
m_NormalIcon = normal;
m_FocusIcon = focus;
m_PressedIcon = pressed;
setIcon(m_NormalIcon);
}
#include "main.moc"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
CoolButton *button = new CoolButton;
//QObject::connect(button, SIGNAL(clicked()), qApp, SLOT(quit()));
button->setIconSize(QSize(40, 40));
button->SetButtonIcons(QIcon("normal.png"), QIcon("focus.png"), QIcon("pressed.png"));
button->show();
return app.exec();
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。