欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++ Qt自定义拨动按钮(有动态效果)

程序员文章站 2022-05-22 13:14:27
...
Qt自定义控件学习

/**
 * @brief  自定义拨动按钮控件,使用时将将Qwidget提升为CustomToggleButton即可
 *         此类的样式:一个删除按钮,动态显示隐藏
 *         应用场景:密码输入,搜索框输入框
 * @author GGX
 * @date   2020-05-20
 */

 

使用方法1:在UI设计拖出一个QLineEdit控件,然后提升

C++ Qt自定义拨动按钮(有动态效果)

效果

C++ Qt自定义拨动按钮(有动态效果)

 

C++ Qt自定义拨动按钮(有动态效果)

头文件

#ifndef CUSTOMTOGGLEBUTTON_H
#define CUSTOMTOGGLEBUTTON_H

#include <QWidget>
#include <QMouseEvent>
#include <QTimer>

namespace Ui {
class CustomToggleButton;
}

class CustomToggleButton : public QWidget
{
    Q_OBJECT

public:
    typedef enum
    {
       AlignLeft = 0, // 按钮在左
       AlignRight

    }ButtonState;

public:
    explicit CustomToggleButton(QWidget *parent = 0);
    ~CustomToggleButton();

    void setTimer(const int msec = 10);
    void setText(const QString &left, const QString &right);
    void setButtonSize(const QSize &size);

    void setState(const CustomToggleButton::ButtonState state);
    uint getState();

signals:
    void sgStateChange(CustomToggleButton::ButtonState state);

private:

    void mousePressEvent(QMouseEvent *event) override;

    void showEvent(QShowEvent *event) override;

public slots:
    void slMove(); //定时器的超时槽函数,用来实现按钮的滑动效果

private:
    Ui::CustomToggleButton *ui;
    QTimer *m_pTimer;
    int m_Msec;

    uint m_ButtonState;
    QSize m_Size;
};

#endif // CUSTOMTOGGLEBUTTON_H
#include "customtogglebutton.h"
#include "ui_customtogglebutton.h"

CustomToggleButton::CustomToggleButton(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CustomToggleButton)
{
    ui->setupUi(this);
    m_pTimer = new QTimer();
    m_Msec = 10;

    m_ButtonState = AlignRight;

    m_Size = QSize(72, 23);

    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(slMove()));
}

CustomToggleButton::~CustomToggleButton()
{
    delete ui;
    delete m_pTimer;
}

/**
 * @brief  设置移动一个像素需要的时间
 * @param  [in] msec 毫秒
 * @return
 * @author GGX
 * @date   2019-10-15
 */
void CustomToggleButton::setTimer(const int msec)
{
    m_Msec = msec;
}

/**
 * @brief  设置按钮显示文字
 * @param  [in] left 左边显示文字
 * @param  [in] right 右边显示文字
 * @return
 * @author GGX
 * @date   2019-10-15
 */
void CustomToggleButton::setText(const QString &left, const QString &right)
{
    ui->label_start->setText(left);
    ui->label_stop->setText(right);
}

/**
 * @brief  设置按钮大小,不建议设置,本身会根据文字自动调整大小
 * @param  [in] size 按钮尺寸
 * @return
 * @author GGX
 * @date   2019-10-15
 */
void CustomToggleButton::setButtonSize(const QSize &size)
{
    m_Size = size;
}

/**
 * @brief  设置按钮状态
 * @param  [in] state 按钮状态
 * @return
 * @author GGX
 * @date   2019-10-15
 */
void CustomToggleButton::setState(const CustomToggleButton::ButtonState state)
{
    m_pTimer->stop();
    m_ButtonState = state;
    if(state == AlignLeft)
    {
        this->setStyleSheet(QString(".QWidget{background-color:rgba(170, 170, 170, 1);spacing:1;min-width:%1;}").arg(m_Size.width()));
        ui->label_start->hide();
        ui->label_stop->show();
        ui->label_move->move(1, ui->label_move->pos().y());
    }
    else
    {
        this->setStyleSheet(QString(".QWidget{background-color:rgba(82, 119, 139, 1);spacing:1;min-width:%1;}").arg(m_Size.width()));
        ui->label_start->setVisible(true);
        ui->label_stop->setVisible(false);
        ui->label_move->move(this->size().width() - ui->label_move->size().width() - 1, ui->label_move->pos().y());
    }

    emit sgStateChange(state);
}

/**
 * @brief  获取按钮状态
 * @param
 * @return 按钮状态
 * @author GGX
 * @date   2019-10-15
 */
uint CustomToggleButton::getState()
{
    return m_ButtonState;
}

void CustomToggleButton::mousePressEvent(QMouseEvent *event)
{
    if(event->button() != Qt::LeftButton)
    {
        return ;
    }

    switch (m_ButtonState)
    {
        case AlignRight:
        case AlignLeft:
             m_pTimer->start(m_Msec);
             break;

        default:
            break;
    }

    ui->label_stop->hide();
    ui->label_start->hide();

    QWidget::mousePressEvent(event);
}

void CustomToggleButton::showEvent(QShowEvent *event)
{
    if(event == NULL)
    {
        return;
    }

    if(!ui->widget->isVisible())
        setState((ButtonState)m_ButtonState);

//    this->adjustSize();
    ui->label_start->adjustSize();
    ui->label_stop->adjustSize();
    ui->label_move->setFixedSize(ui->label_start->size());
    m_Size = QSize(ui->label_move->size().width() + ui->label_start->size().width() + 2, 23);
//    ui->label_move->setStyleSheet(QString(".QLabel{min-width:%1;}").arg(ui->label_move->size().width()));
    this->setFixedSize(m_Size);
}

/**
 * @brief  按钮移动槽函数
 * @param
 * @return
 * @author GGX
 * @date   2019-10-15
 */
void CustomToggleButton::slMove()
{
    switch(m_ButtonState)
    {
    case AlignLeft:

        if(ui->label_move->pos().x() + ui->label_move->size().width() + 2 > this->size().width())
        {
            setState(AlignRight);
            return ;
        }

        ui->label_move->move(ui->label_move->pos().x() + 1,  ui->label_move->pos().y());

        break;

    case AlignRight:

        if(ui->label_move->pos().x() < 1)
        {
            setState(AlignLeft);
            return;
        }
        ui->label_move->move(ui->label_move->pos().x() - 1, ui->label_move->pos().y());
        break;
    }
}

 

相关标签: QT C/C++程序