QT 做的数字上下滑动选择器

该代码实现了一个名为NumWidget的自定义数值选择组件,继承自QWidget。它支持设置范围、当前值,并通过鼠标事件改变数值。在鼠标按下、移动和释放时,数值会根据鼠标移动位置相应增减。在paintEvent中绘制数值,包括当前值和相邻的两个数值。

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

#ifndef NUMBERWIDGET_H
#define NUMBERWIDGET_H

#include <QWidget>
#include <QPainter>
#include <QMouseEvent>
#include "Public/public_function.h"
class NumWidget : public QWidget
{
    Q_OBJECT
public:
    explicit NumWidget(QWidget *parent = nullptr):QWidget(parent)
    {
    }
    void setRange(int num1,int num2)
    {
        min = num1;
        max = num2;
    }

    void setCurrentVlaue(int num)
    {
        currentvlaue = num;
        update();
    }

    int  getCurrentVlaue()
    {
        return currentvlaue;
    }

protected:
    void paintEvent(QPaintEvent *event)
    {
        Q_UNUSED(event);
        QString str;
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing,true);
        painter.setBrush(QBrush(Qt::white));
        int size = this->height()*2/5 -qAbs((this->height() - qAbs(mousemovepos)) /4);
        painter.setFont(Public_Function::getFont(size));
        painter.setPen(QPen(Qt::gray));
        for(int a=0;a <2;a++)
        {
            if(currentvlaue -a >min)
            {
                str = (currentvlaue -a -1)<10?(QString::number(0) +QString::number(currentvlaue -a -1)) :QString::number(currentvlaue -a -1);
                painter.drawText(0,-a*height()/3 +mousemovepos,this->width(),this->height()/3,Qt::AlignCenter,str);
            }
            else
            {
                str = (currentvlaue -a+max)<10?(QString::number(0) +QString::number(currentvlaue -a+max)) :QString::number(currentvlaue -a+max);
                painter.drawText(0,-a*height()/3 +mousemovepos,this->width(),this->height()/3,Qt::AlignCenter,str);
            }

            if(currentvlaue +a <max)
            {
                str = (currentvlaue +a +1)<10?(QString::number(0) +QString::number(currentvlaue +a +1)) :QString::number(currentvlaue +a +1);
                painter.drawText(0,a*height()/3 +height()*2/3 +mousemovepos,this->width(),this->height()/3,Qt::AlignCenter,str);
            }
            else
            {
                str = (currentvlaue +a -max)<10?(QString::number(0) +QString::number(currentvlaue +a -max)) :QString::number(currentvlaue +a -max);
                painter.drawText(0,a*height()/3 +height()*2/3 +mousemovepos,this->width(),this->height()/3,Qt::AlignCenter,str);
            }
        }
        painter.setPen(QPen(Qt::black));
        size = qAbs((this->height() - qAbs(mousemovepos)) /4);
        painter.setFont(Public_Function::getFont(size));
        str = (currentvlaue)<10?(QString::number(0) +QString::number(currentvlaue)) :QString::number(currentvlaue);
        painter.drawText(0,height()/3 +mousemovepos,this->width(),this->height()/3,Qt::AlignCenter,str);
    }

    void mousePressEvent(QMouseEvent *event)
    {
        mousepresspos = event->globalPos();
    }

    void mouseMoveEvent(QMouseEvent *event)
    {
        mousemovepos = event->globalPos().y() -mousepresspos.y();
        if(mousemovepos > height()/3)
        {
            currentvlaue -=1;
            if(currentvlaue ==min)currentvlaue =0;
            else if(currentvlaue <min)currentvlaue +=(max+1);
            mousepresspos = event->globalPos();
        }
        else if(qAbs(mousemovepos) >height()/3)
        {
            currentvlaue +=1;
            if(currentvlaue ==max)currentvlaue =max;
            else if(currentvlaue >max)currentvlaue -=(max +1);
            mousepresspos = event->globalPos();
        }
        update();
    }

    void mouseReleaseEvent(QMouseEvent *)
    {
        if(mousemovepos >=0)
        {
            if(mousemovepos*3%this->height() >= this->height()/3)
                currentvlaue -=1;
            if(currentvlaue ==min)currentvlaue =0;
            else if(currentvlaue <min)currentvlaue +=(max+1);
        }
        else
        {
            if(qAbs(mousemovepos*3%this->height()) >= this->height()/3)
                currentvlaue +=1;
            if(currentvlaue ==max)currentvlaue =max;
            else if(currentvlaue >max)currentvlaue -=(max +1);
        }
        mousemovepos=0;
        update();
    }

signals:

private:
    int min =0;
    int max=59;
    int currentvlaue =(max +min)/2;
    int mousemovepos =0;
    QPoint mousepresspos;
};
#endif // NUMBERWIDGET_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值