选择按钮

本文介绍了一个自定义的开关按钮组件的实现方式,该组件使用Qt进行开发,通过动画效果模拟开关状态的变化。文章详细展示了如何加载不同的图片资源来表示开关的不同状态,并利用定时器触发动画效果,实现平滑过渡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "switchbutton.h"
#include <QTimer>
#include <QPainter>
#include <QDebug>
#include <QPaintEvent>
#include <QBitmap>
#include <QPixmap>
SwitchButton::SwitchButton(QWidget *parent) :
    QPushButton(parent),state(false),_pos(0),isReady(true)
{
    closeTimer = new QTimer(this);
    connect(closeTimer,SIGNAL(timeout()),this,SLOT(closeAnimation()));
    openTimer = new QTimer(this);
    connect(openTimer,SIGNAL(timeout()),this,SLOT(openAnimation()));

    onPixmap = new QPixmap(":/image/on-slide1.png");
    offPixmap = new QPixmap(":/image/off-slide1.png");
    on_offPixmap = new QPixmap(":/image/on-off-slide1.png");

    resize(onPixmap->size());
    this->shortcut();
}

bool SwitchButton::getState()
{
    return state;
}

void SwitchButton::setState(bool _state)
{
    state = _state;
    emit stateChange(state);

    if(state)
    {
        if(_pos < 0)
            _pos = 0;
        openTimer->start(10);
        closeTimer->stop();
    }
    else
    {
        openTimer->stop();
        closeTimer->start(10);
    }
    isReady = false;
}

void SwitchButton::setStateWithoutAnimation(bool _state)
{
    state = _state;
    emit stateChange(state);

    if(state)
    {
        //_pos = buttonSize.width() - on_offPixmap->width();
        _pos = 97 - on_offPixmap->width();
    }
    else
    {
        _pos = 0;
    }

    update();
}

void SwitchButton::mousePressEvent(QMouseEvent *e)
{
    changeState();
    QPushButton::mousePressEvent(e);
}

void SwitchButton::paintEvent(QPaintEvent *e)
{
    buttonSize = this->size();
    QPainter painter(this);
    //painter.drawPixmap(0,0,100,100,QPixmap("://ui/11.png"));

    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);

    int _width = buttonSize.width();
    int _height = buttonSize.height();

    if(_pos < 0)
    {
        if(state)
            _pos = 97 - on_offPixmap->width();
        else
            _pos = 0;
    }
    double _scale = double(_pos+3) / double(_width);
    painter.drawPixmap(0,0,_width,_height,*offPixmap);
    painter.drawPixmap(QRect(0,0,_pos+3,_height),onPixmap->copy(0,0,_scale * onPixmap->width(),onPixmap->height()));
    painter.drawPixmap(_pos,0,_height * 56.0/30.0,_height,*on_offPixmap);
}

void SwitchButton::setButtonSize(int w, int h)
{
    buttonSize.setWidth(w);
    buttonSize.setHeight(h);
    buttonSize = this->size();
    update();
}

void SwitchButton::changeState()
{
    if(state == true)
    {
        //        openTimer->stop();
        //        closeTimer->start(10);
        //        state = 0;
        setState(false);
    }
    else
    {
        //        openTimer->start(10);
        //        closeTimer->stop();
        //        state = 1;
        setState(true);
    }
    isReady = false;
}

void SwitchButton::closeAnimation()
{
    _pos -= 3;
    if(_pos <= 0)
    {
        _pos = 0;
        closeTimer->stop();
        isReady = true;
        repaint();
        return;
    }
    update();
}

void SwitchButton::openAnimation()
{
    _pos += 3;
    if(_pos < 0)
        _pos = 0;

    if(_pos >= buttonSize.width() - on_offPixmap->width())
    {
        _pos = buttonSize.width() - on_offPixmap->width();
        openTimer->stop();
        isReady = true;
        repaint();
        return;
    }
    update();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值