Qt数字滚动器

2020-03-06  本文已影响0人  Caiaolun

原文地址: https://blog.csdn.net/gdizcm/article/details/61915344

#ifndef VERTICALSCROLL_H
#define VERTICALSCROLL_H
 
#include <QWidget>
#include <QPropertyAnimation>
#include <QPainter>
 
namespace Ui {
 
class VerticalScroll;
 
}
 
class VerticalScroll : public QWidget
 
{
 
    Q_OBJECT
 
    Q_PROPERTY(int deviation READ readDeviation WRITE setDeviation )
 
public:
 
    explicit VerticalScroll(QWidget *parent = 0);
 
    ~VerticalScroll();
    //设置范围
    void setRange(double min, double max);
    //获取当前值
    double readValue();
 
protected:
    void mousePressEvent(QMouseEvent *);
 
    void mouseMoveEvent(QMouseEvent *);
 
    void mouseReleaseEvent(QMouseEvent *);
 
    void wheelEvent(QWheelEvent *);
 
    void paintEvent(QPaintEvent *);
    //描绘数字
    void paintNum(QPainter &painter, double num, int deviation);
    //使选中的数字回到屏幕中间
    void homing();
    //鼠标移动偏移量,默认为0
    int readDeviation();
    //设置偏移量
    void setDeviation(int n);
 
signals:
 
    void currentValueChanged(double value);
 
    void deviationChange(double deviation);
 
private:
 
    Ui::VerticalScroll *ui;
 
private:
    double m_minRange;      //最小值
    double m_maxRange;      //最大值
    double m_currentValue;  //当前选中的值
    bool isDragging;        //鼠标是否按下
    int m_deviation;        //偏移量,记录鼠标按下后移动的垂直距离
    int m_mouseSrcPos;
    int m_numSize;
    QPropertyAnimation *homingAni;
    const double interval;  //间隔大小
    const int devide;       //分隔数量
};
 
#endif // VERTICALSCROLL_H
#include "verticalscroll.h"
#include "ui_verticalscroll.h"
#include <QMouseEvent>
#include <QDebug>
 
VerticalScroll::VerticalScroll(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VerticalScroll),
    m_minRange(0),      //最小值默认为0
    m_maxRange(100),    //最大值默认100
    m_currentValue(50), //当前值默认50
    isDragging(false),
    m_deviation(0),     //默认偏移量为0
    m_numSize(12),
    interval(0.5),      //间隔默认0.5
    devide(7)           //默认分成7格
{
 
    ui->setupUi(this);
 
    homingAni = new QPropertyAnimation(this, "deviation");
    homingAni->setDuration(300);
    homingAni->setEasingCurve(QEasingCurve::OutQuad);
}
 
VerticalScroll::~VerticalScroll()
{
    delete ui;
}
/*
 * 设置范围
 * double min 最小值
 * double max 最大值
*/
void VerticalScroll::setRange(double min, double max)
{
    m_minRange = min;
    m_maxRange = max;
    if (m_currentValue < min)
    {
        m_currentValue = min;
    }
    if (m_currentValue > max)
    {
        m_currentValue = max;
    }
    repaint();
}
//获取当前值
double VerticalScroll::readValue()
{
    return m_currentValue;
}
 
void VerticalScroll::mousePressEvent(QMouseEvent *e)
{
    qDebug()<<"mouse pressed on vertical scroll";
 
    homingAni->stop();
    isDragging = true;
    m_mouseSrcPos = e->pos().y();
    QWidget::mousePressEvent(e);
}
 
void VerticalScroll::mouseMoveEvent(QMouseEvent *e)
{
    if (isDragging)
    {
        if ((m_currentValue == m_minRange && e->pos().y() >= m_mouseSrcPos) ||
                ( m_currentValue == m_maxRange && e->pos().y() <= m_mouseSrcPos ))
        {
            return;
        }
 
        m_deviation = e->pos().y() - m_mouseSrcPos;
        //若移动速度过快,则进行限制
        if (m_deviation > (height() - 1) / devide)
        {
            m_deviation = (height() - 1) / devide;
        }
        else if (m_deviation < -(height() - 1) / devide)
        {
            m_deviation = -( height() - 1) / devide;
        }
 
        emit deviationChange((double)m_deviation / ((height() - 1) / devide));
        repaint();
    }
}
 
void VerticalScroll::mouseReleaseEvent(QMouseEvent *)
{
    if (isDragging)
    {
        isDragging = false;
        homing();
    }
}
 
void VerticalScroll::wheelEvent(QWheelEvent *e)
{
    if (e->delta() > 0)
    {
        m_deviation = (this->height() - 1) / devide;
    }
    else
    {
        m_deviation = -(this->height() - 1) / devide;
    }
    homing();
    repaint();
}
 
void VerticalScroll::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    int Height = height() - 1;
 
    if ( m_deviation >= Height / devide && m_currentValue > m_minRange )
    {
        m_mouseSrcPos += Height / devide;
        m_deviation -= Height / devide;
        m_currentValue -= interval;
    }
 
    if ( m_deviation <= -Height / devide && m_currentValue < m_maxRange )
    {
        m_mouseSrcPos -= Height / devide;
        m_deviation += Height / devide;
        m_currentValue += interval;
    }
 
    //middle number
    paintNum(painter, m_currentValue, m_deviation);
 
    //两侧数字1
    if (m_currentValue != m_minRange)
    {
        paintNum(painter, m_currentValue - interval, m_deviation - Height / devide);
    }
 
    if (m_currentValue != m_maxRange)
    {
        paintNum(painter, m_currentValue + interval, m_deviation + Height / devide);
    }
 
    for (int i=2; i <= devide/2; ++i)
    {
        if (m_currentValue - interval * i >= m_minRange)
        {
            paintNum(painter, m_currentValue - interval * i, m_deviation - Height / devide * i);
        }
 
        if (m_currentValue + interval * i <= m_maxRange)
        {
            paintNum(painter, m_currentValue + interval * i, m_deviation + Height / devide * i);
        }
    }
}
/*
 * 根据偏移量描绘数字
 * double num 需要显示的数字
 * int deviation 数字相对中间的偏移量
*/
void VerticalScroll::paintNum(QPainter &painter, double num, int deviation)
{
    int Width = width() - 1;
    int Height = height() - 1;
    int size = (Height - qAbs(deviation)) / m_numSize; //偏移量越大,数字越小
    int transparency = 255 - 255 * qAbs(deviation) / Height;
    int height = Height / devide;
    int y = Height / 2 + deviation - height / 2;
 
    QFont font;
    font.setPixelSize(size);
    painter.setFont(font);
    painter.setPen(QColor(255, 2,2,transparency));
 
    if ( y >= 0 && y + height < Height)
    {
        painter.drawRect(0, y, Width, height);
        painter.drawText(QRectF(0, y, Width-20, height),
                         Qt::AlignCenter,
                         QString::number(num, 'f', 1));
    }
}
 
/*
 * 使选中的数字回到屏幕中间
*/
void VerticalScroll::homing()
{
    if ( m_deviation > height() / 10)
    {
        homingAni->setStartValue( ( height() - 1 ) / 8 - m_deviation);
        homingAni->setEndValue(0);
        m_currentValue -= interval;
    }
    else if ( m_deviation > -height() / 10 )
    {
        homingAni->setStartValue(m_deviation);
        homingAni->setEndValue(0);
    }
    else if ( m_deviation < -height() / 10 )
    {
        homingAni->setStartValue(-(height() - 1) / 8 - m_deviation);
        homingAni->setEndValue(0);
        m_currentValue += interval;
    }
 
    emit currentValueChanged(m_currentValue);
    homingAni->start();
}
 
int VerticalScroll::readDeviation()
{
    return m_deviation;
}
 
void VerticalScroll::setDeviation(int n)
{
    m_deviation = n;
    repaint();
}
上一篇下一篇

猜你喜欢

热点阅读