0

我正在显示来自带有QProgressBar.

传感器值的范围可以从0 to 0.5

为了在 上显示这些值QProgressBar,我0-0.5通过乘以 100 将范围 ( ) 转换为整数范围

[0 , 0.5] -> [0 , 50]

 ui->progressBar->setRange(0, 0.5*100);

 ui->progressBar->setValue(sensor_value*100.0);

这样当收到一个新值时,我将它乘以 100 ( 0.12*100 = 12/50)

现在我要QDoubleSpinBox为 选择一个“阈值”值QProgressBar,如下所示:

在此处输入图像描述

为了将值转换为阈值并在达到阈值QDoubleSpinBox时使用此阈值生成声音,我做了以下操作QProgressBar

threshold = ((ui->doubleSpinBox->value()*100)/50); //---> not sure if this is right

ui->label->setText(QString::number(threshold, 'f',2 ));

if(sensor_value > threshold)
       {
           ui->rdo_btn_vertical->show();
           ui->rdo_btn_vertical->setStyleSheet(StyleSheetOn1);
           QSound::play(":/resources/beep1.wav");
       }

但是,这里的问题是我在上述计算中没有得到正确的阈值。所以我无法比较 QProgressBar 上显示的阈值和实际传感器值有人可以在那里发现一些问题吗?

4

1 回答 1

0

小部件.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    double threshold;
    ~Widget();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

小部件.cpp

#include "widget.h"
#include "ui_widget.h"
#include<QLabel>
#include<QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->progressBar->setRange(0, 0.5*100);
    ui->senser->setRange(0,0.5);
    ui->senser->setSingleStep(0.01);

    //setting threshold value from threshold spin box
    connect(ui->thresholdSpinBox,QOverload<double>::of(&QDoubleSpinBox::valueChanged),[&](){
         threshold = ((ui->thresholdSpinBox->value()*100)/50);
    });


    // i am taking senser value from another double spin box manually
    connect(ui->senser,QOverload<double>::of(&QDoubleSpinBox::valueChanged),this,[&]()
    {
            ui->progressBar->setValue(ui->senser->value()*100);
            if(ui->senser->value() > threshold)
               ui->notifier_label->setText("alert greator than threshold");
            QTimer::singleShot(3000,[=](){ui->notifier_label->clear();});

    });
}

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



像这样设置用户界面并运行这段代码,也许你会得到你的答案 在此处输入图像描述

于 2020-08-25T15:42:27.473 回答