QDoubleSpinBox的使用示例

QDoubleSpinBox即可以做为数值型输入框使用,也可以使用只读型数据显示框,在作为输入框使用时比QLineEdit有以下几个方面的优势
1.可以设置范围,并且范围精确,
2.输入数据精确,自动屏幕非数值以外的字符。
3.设置步长后可以通过上下键自动增减值。
本篇记录QDoubleSpinBox的基本使用。

1.QDoubleSpinBox的基本示例,设置QDoubleSpinBox的范围为0-100.0,步长为10
ui->doubleSpinBox->setRange(0, 100.0);          //设置范围
ui->doubleSpinBox->setSingleStep(10.0);         //设置步长
这个时候可以通过手动输入0-100之内的浮点数,也可以按键盘上下键增减数值

QDoubleSpinBox的使用示例_第1张图片

这里是初始化就已经设置的范围及步长,其实我们也可以随时改变他的范围及步长,例如:

ui->doubleSpinBox->setRange(50, 60);    //设置范围50-60
ui->doubleSpinBox->setSingleStep(0.5);  //设置步长0.5
ui->doubleSpinBox->setValue(55.2);      //设置默认值为55.2

QDoubleSpinBox的使用示例_第2张图片

2.有时候为了美观,我们并不需要右边的按钮,并且输入框界面也要需要按自动的要求美化

ui->doubleSpinBox_2->setRange(-50.0, 50.0);     //设置范围
ui->doubleSpinBox_2->setSingleStep(10.0);       //设置步长
QString style2 = QString("QDoubleSpinBox{background-color:#E9E9EA;color:#6D6E6B;border-radius:5px;line-height: 20px;}"
                             "QDoubleSpinBox:checked{background-color:#E9E9EA;border-radius:5px;border:2px solid rgba(47, 137, 252, 1);}"
                             "QDoubleSpinBox:hover{background-color:#E9E9EA;border-radius:5px;border:2px solid rgba(47, 137, 252, 1);}"
                             "QDoubleSpinBox:focus{background-color:#E9E9EA;border-radius:5px;border:2px solid rgba(47, 137, 252, 1);}"
                             "QDoubleSpinBox:disabled{background-color:#F0F0F0;border-radius:5px;}"
                             );
ui->doubleSpinBox_2->setStyleSheet(style2);
ui->doubleSpinBox_2->setButtonSymbols(QDoubleSpinBox::NoButtons); //隐藏右边上下箭头按钮

QDoubleSpinBox的使用示例_第3张图片

3.如果我们想按上下键自动真增减数值,会出现自动选择数据,并且光标在输入的第一个位置:

QDoubleSpinBox的使用示例_第4张图片

那么我们如果来解决这两个问题呢

1.QDoubleSpinBox里有lineEdit对象,调用deselect();即可去掉选中颜色,

2.通过重新定位光标位置来解决第二个问题。

因此重写QDoubleSpinBox的stepBy函数。

#ifndef MYDOUBLESPINBOX_H
#define MYDOUBLESPINBOX_H

#include 

class MyDoubleSpinBox: public QDoubleSpinBox
{
    Q_OBJECT
public:
    MyDoubleSpinBox(QWidget *parent = nullptr);

    void stepBy(int steps) override;
    void setRangeDec(double min, double max, int decimals = 0);

};

#endif // MYDOUBLESPINBOX_H
#include "mydoublespinbox.h"
#include 
#include 

MyDoubleSpinBox::MyDoubleSpinBox(QWidget *parent)
    :QDoubleSpinBox(parent)
{

}

void MyDoubleSpinBox::stepBy(int steps)
{
    QDoubleSpinBox::stepBy(steps);
    lineEdit()->deselect();//去掉高亮显示颜色
    lineEdit()->setCursorPosition(lineEdit()->text().size());//设置光标位置在最右边
}

void MyDoubleSpinBox::setRangeDec(double min, double max, int decimals)
{
    this->setRange(min, max);
    this->setDecimals(decimals);
}

QDoubleSpinBox的使用示例_第5张图片

完整代码如下:

#ifndef QDOUBLESPINBOXDEMO_H
#define QDOUBLESPINBOXDEMO_H

#include 

QT_BEGIN_NAMESPACE
namespace Ui { class QDoubleSpinBoxDemo; }
QT_END_NAMESPACE

class QDoubleSpinBoxDemo : public QDialog
{
    Q_OBJECT

public:
    QDoubleSpinBoxDemo(QWidget *parent = nullptr);
    ~QDoubleSpinBoxDemo();

    void initView();
    void initData();

public slots:
    void slotChangeRange();
    void slotChangeRange2();
    void slotChangeRange3();
    void slotEditingFinished();

private:
    Ui::QDoubleSpinBoxDemo *ui;
};
#endif // QDOUBLESPINBOXDEMO_H
#include "qdoublespinboxdemo.h"
#include "ui_qdoublespinboxdemo.h"
#include 
#include 

QDoubleSpinBoxDemo::QDoubleSpinBoxDemo(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::QDoubleSpinBoxDemo)
{
    ui->setupUi(this);
    initView();
    initData();
}

QDoubleSpinBoxDemo::~QDoubleSpinBoxDemo()
{
    delete ui;
}


void QDoubleSpinBoxDemo::initView()
{
    QString style = QString("QDoubleSpinBox{padding-right:15px;}"
                            "QDoubleSpinBox::up-button{subcontrol-origin: border;subcontrol-position: top right;width: 16px;border-image: url(:/image/spinup.png);border-width: 1px;}"
                            "QDoubleSpinBox::up-button:hover{border-image: url(:/image/spinup_hover.png);}"
                            "QDoubleSpinBox::up-button:pressed{border-image: url(:/image/spinup_pressed.png);}"
                            "QDoubleSpinBox::down-button{subcontrol-origin: border;subcontrol-position: bottom right;width: 16px;border-image: url(:/image/spindown.png);border-width: 1px;border-top-width: 0;}"
                            "QDoubleSpinBox::down-button:hover{border-image: url(:/image/spindown_hover.png);}"
                            "QDoubleSpinBox::down-button:pressed{border-image: url(:/image/spindown_pressed.png);}");
    ui->doubleSpinBox->setStyleSheet(style);

    QString style2 = QString("QDoubleSpinBox{background-color:#E9E9EA;color:#6D6E6B;border-radius:5px;line-height: 20px;padding-left:4px;}"
                             "QDoubleSpinBox:checked{background-color:#E9E9EA;border-radius:5px;border:2px solid rgba(47, 137, 252, 1);}"
                             "QDoubleSpinBox:hover{background-color:#E9E9EA;border-radius:5px;border:2px solid rgba(47, 137, 252, 1);}"
                             "QDoubleSpinBox:focus{background-color:#E9E9EA;border-radius:5px;border:2px solid rgba(47, 137, 252, 1);}"
                             "QDoubleSpinBox:disabled{background-color:#F0F0F0;border-radius:5px;}"
                             );
    ui->doubleSpinBox_2->setStyleSheet(style2);
    ui->doubleSpinBox_2->setButtonSymbols(QDoubleSpinBox::NoButtons); //隐藏右边上下箭头按钮
    QString style3 = QString("background:#ffffff;border-radius:5px;color: #666666; line-height: 20px;");//font-size: 14px;font-weight: 400;
    ui->doubleSpinBox_3->setStyleSheet(style2);
    ui->doubleSpinBox_3->setButtonSymbols(QDoubleSpinBox::NoButtons); //隐藏右边上下箭头按钮

    QString strLineEditStyle = QString("QLineEdit{background-color:#E9E9EA;color:#6D6E6B;border-radius:5px;}"
                                        "QLineEdit:hover{background-color:#E9E9EA;border-radius:5px;border:2px solid rgba(47, 137, 252, 1);}"
                                        "QLineEdit:focus{background-color:#E9E9EA;border-radius:5px;border:2px solid rgba(47, 137, 252, 1);}"
                                        "QLineEdit:disabled{background-color:#F0F0F0;border-radius:5px;}");

    ui->lineEdit->setStyleSheet(strLineEditStyle);
}

void QDoubleSpinBoxDemo::initData()
{
    ui->doubleSpinBox->setRange(0, 100.0);          //设置范围
    ui->doubleSpinBox->setSingleStep(10.0);         //设置步长
    ui->doubleSpinBox_2->setRange(-50.0, 50.0);     //设置范围
    ui->doubleSpinBox_2->setSingleStep(10.0);       //设置步长
    ui->doubleSpinBox_3->setRange(50.0, 60.0);      //设置范围
    ui->doubleSpinBox_3->setSingleStep(0.5);        //设置步长
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(slotChangeRange()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(slotChangeRange2()));
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(slotChangeRange3()));
    connect(ui->doubleSpinBox_3, SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
}

void QDoubleSpinBoxDemo::slotChangeRange()
{
    qDebug() << "slotChangeRange===========================";
    ui->doubleSpinBox->setRange(50, 60);    //设置范围50-60
    ui->doubleSpinBox->setSingleStep(0.5);  //设置步长0.5
    ui->doubleSpinBox->setValue(55.2);      //设置默认值为55.2
}

void QDoubleSpinBoxDemo::slotChangeRange2()
{
    qDebug() << "slotChangeRange2===========================";
}

void QDoubleSpinBoxDemo::slotChangeRange3()
{
    qDebug() << "slotChangeRange3===========================";
    ui->doubleSpinBox_3->setRangeDec(30.0, 80.0, 1);      //设置范围
}

void QDoubleSpinBoxDemo::slotEditingFinished()
{
    qDebug() << "slotEditingFinished===========================" << ui->doubleSpinBox_3->value();
}

QDoubleSpinBox的使用示例_第6张图片

你可能感兴趣的:(QT,QT)