Qt 自定义PushButton

功能:鼠标弹起并在按键区域内时,按键响应。并实现normal、hover、pressed效果,PushButton大小默认为传入图片大小。

源码:cpp

#include "pushbutton.h"

#include <QPainter>
#include <QMouseEvent>
#include <QFontMetrics>
#include <QLabel>

PushButton::PushButton(QString normal, QString hover, QString pressed, QWidget *parent) :
    QPushButton(parent)
{
    buttonState = Normal;

    normalPixmap.load(normal);
    hoverPixmap.load(hover);
    pressPixmap.load(pressed);

    this->setFixedSize(normalPixmap.size());

    this->setContentsMargins(0, 0, 0, 0);
}

PushButton::PushButton(QString background, QWidget *parent) :
    QPushButton(parent)
{
    buttonState = Normal;

    normalPixmap.load(background);
    hoverPixmap.load(background);
    pressPixmap.load(background);

    this->setFixedSize(normalPixmap.size());

    this->setContentsMargins(0, 0, 0, 0);
}

PushButton::~PushButton()
{

}

void PushButton::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    switch(buttonState)
    {
    case Normal:
        painter.drawPixmap(this->rect(), normalPixmap);
        break;
    case Hover:
        painter.drawPixmap(this->rect(), hoverPixmap);
        break;
    case Pressed:
        painter.drawPixmap(this->rect(), pressPixmap);
    }

    painter.drawText(this->rect(), Qt::AlignCenter, this->text());
}

void PushButton::enterEvent(QEvent *)
{
    buttonState = Hover;
    update();
}

void PushButton::leaveEvent(QEvent *)
{
    buttonState = Normal;
    update();
}

void PushButton::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        buttonState = Pressed;
        update();
    }
}

bool PushButton::isOnPushButton(const QPoint &point, const PushButton *pushButton)
{
    if(point.x() < 0 || point.x() > pushButton->width() ||
            point.y() < 0 || point.y() > pushButton->height())
    {
        return false;
    }
    return true;
}

void PushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        //判断鼠标抬起时是否在PushButton之上
        if(isOnPushButton(e->pos(), this))
        {
            emit clicked();
        }

        buttonState = Hover;
        update();
    }
}

.h文件

#ifndef PUSHBUTTON_H
#define PUSHBUTTON_H
#include <QPushButton>

#define Normal 0
#define Hover 1
#define Pressed 2
class QPixmap;

class PushButton : public QPushButton
{
    //Q_OBJECT
public:
    explicit PushButton(QString normal, QString hover, QString pressed, QWidget *parent=0);
    explicit PushButton(QString background, QWidget *parent=0);
    ~PushButton();
    bool isOnPushButton(const QPoint &point, const PushButton *pushButton);

protected:
    void paintEvent(QPaintEvent *);
    void enterEvent(QEvent *);
    void leaveEvent(QEvent *);
    void mousePressEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
    //void loadPixmap(QString path);

private:
    char buttonState;
    QPixmap normalPixmap;
    QPixmap hoverPixmap;
    QPixmap pressPixmap;
};

#endif // PUSHBUTTON_H

效果:





eg:

PushButton *xxx = new PushButton(":/image/contact.png",":/image/cut-the-rope.png",":/image/drop.png",this);

以上 留存...

出处:Qt 自定义PushButton





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值