qt 自定义样式 switch开关,已解决

 

        在日常需求中,需要对功能增加一个开关,因此做了简单封装。结果能正常使用。自定义信号接收!

实现

	QWidget* switchBtn = new CCendSwitchWidget(btn_value);
	connect(switchBtn, SIGNAL(clicked(bool,QString)), this, SLOT(clickedSlot(bool, QString)));

 头文件


#ifndef C_CEND_SWITCH_WIDGET_H
#define C_CEND_SWITCH_WIDGET_H

#include <QWidget>
#include <QPainter>
#include <QEvent>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QVariantAnimation>
#include <QMap>
#include <QGuiApplication>
#include <string>
using std::string;

class CCendSwitchWidget : public QWidget
{
    Q_OBJECT

public:
    CCendSwitchWidget(QString,QWidget* parent = 0);
    QSize sizeHint() const override;
    bool isOpened() const;

signals:
    void clicked(bool,QString);

private:
    void setButtonPos(float where);
    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void enterEvent(QEvent *event);
    void leaveEvent(QEvent *event);

private:
    bool opened;
    bool hover;
    float atWhere; /* [0, 1] */
    QPoint pressPt;

    QString m_pDataType;
};

#endif //C_CEND_SWITCH_WIDGET_H

cpp

#include "CCendSwitchWidget.h"

CCendSwitchWidget::CCendSwitchWidget(QString type,QWidget* parent) :
    QWidget(parent)
{
    m_pDataType = type;
    opened = false;
    hover = false;
    atWhere = 0;
}

void CCendSwitchWidget::setButtonPos(float where)
{
    atWhere = where;
    update();
}

bool CCendSwitchWidget::isOpened() const
{
    return opened;
}

void CCendSwitchWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    qreal rad = height() * 0.5; /* 圆半径 */
    int xleft = rad;
    int xright = width() - rad;
    int xpos = xleft + (xright - xleft) * atWhere;
    painter.setPen(Qt::NoPen);
    painter.setBrush((xpos == xright) ? QColor(0, 170, 255) : QColor(173, 173, 173));
    painter.drawRoundedRect(0, 0, width(), height(), rad, rad);
    if (hover) /* 鼠标悬停稍微提亮一点 */
    {
        painter.setBrush(QColor(255, 255, 255, 63));
        painter.drawRoundedRect(0, 0, width(), height(), rad, rad);
    }
    painter.setPen(QColor(213, 213, 213));
    painter.setBrush(QColor(243, 243, 243));
    painter.drawEllipse(QPointF(xpos, rad), rad - 1, rad - 1);
}

void CCendSwitchWidget::mousePressEvent(QMouseEvent *event)
{
    pressPt = event->pos();
}

void CCendSwitchWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if (pressPt == event->pos())
    {
        opened = !opened;
        emit clicked(opened, m_pDataType);
        QVariantAnimation* ani = new QVariantAnimation(this);
        ani->setStartValue(0.0f);
        ani->setEndValue(1.0f);
        ani->setDuration(200);
        ani->setDirection(opened ? QVariantAnimation::Forward : QVariantAnimation::Backward);
        connect(ani, &QVariantAnimation::valueChanged, this,
            [this](const QVariant& value) { setButtonPos(value.toFloat()); });
        ani->start(QAbstractAnimation::DeleteWhenStopped);
    }
}

void CCendSwitchWidget::enterEvent(QEvent *event)
{
    hover = true;
    update();
}

void CCendSwitchWidget::leaveEvent(QEvent *event)
{
    hover = false;
    update();
}

QSize CCendSwitchWidget::sizeHint() const
{
    return QSize(21, 10);
}

 最准效果

参考

https://www.cnblogs.com/mengxiangdu/p/16926176.html

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 14:44:05       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 14:44:05       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 14:44:05       57 阅读
  4. Python语言-面向对象

    2024-07-19 14:44:05       68 阅读

热门阅读

  1. PyTorch深度学习入门汇总

    2024-07-19 14:44:05       24 阅读
  2. 非结构化网格跨结点并行渲染

    2024-07-19 14:44:05       20 阅读
  3. 快速删除node_modules

    2024-07-19 14:44:05       19 阅读
  4. 小抄 20240714 我即世界

    2024-07-19 14:44:05       20 阅读
  5. 二叉树---找树左下角的值

    2024-07-19 14:44:05       18 阅读
  6. Android 一体机等root后的机器指令截屏

    2024-07-19 14:44:05       21 阅读
  7. 【题解】StarryCoding P211 勇闯高塔

    2024-07-19 14:44:05       22 阅读
  8. Linux 之 设置环境变量

    2024-07-19 14:44:05       24 阅读
  9. 做一只勤劳的小蜜蜂

    2024-07-19 14:44:05       20 阅读
  10. 【ubuntu 网卡混杂模式设置】

    2024-07-19 14:44:05       17 阅读
  11. Hive函数之-posexplode()

    2024-07-19 14:44:05       14 阅读