Search code examples
c++qtqdial

QDial set range from 0.00 to 10.00 to widget displays


I have a class that connects a Lcd display with a dial and when you rotate the dial you get values on the lcd like this

#ifndef SLIDELCD_H
#define SLIDELCD_H

#include <QDial>
#include <QVBoxLayout>
#include <QLCDNumber>
#include "CustomDial.h"
class SlideLcd: public QWidget
{
Q_OBJECT
public:
SlideLcd(QWidget *parent=nullptr);
private:
CustomDial *dial;
QLCDNumber *lcd;
QVBoxLayout *layout;
};

SlideLcd::SlideLcd(QWidget *parent)
               :QWidget(parent)
{
dial = new CustomDial;
dial->setNotchesVisible(true);
lcd = new QLCDNumber;
connect(dial, SIGNAL(valueChanged(int)), lcd , SLOT(display(int)));
layout = new QVBoxLayout;
layout->addWidget(lcd);
layout->addWidget(dial);
setLayout(layout);
}

I know that with QDial::setRange(0,100) you can set the range from 0-100 but is there any way to set ranges like 0.00 to 100.00


Solution

  • Given that there's no way to specify QDial range using double, you should provide an extra slot to catch the valueChanged signal, edit the value there and pass the edited value to display method of lcd.

    So, in your widget class:

    private slots:
      void dialValueChanged(int value);
    

    Set dial range to 0-10000 in constructor:

    dial->setMinimum(0);
    dial->setMaximum(10000);
    

    then connect the new slot:

    connect(dial, SIGNAL(valueChanged(int)), this , SLOT(dialValueChanged(int)));
    

    The slot definition is like this:

    void SlideLcd::dialValueChanged(int value)
    {
      double v = (double)((double)value / 100);
      lcd->display(v);
    }
    

    This way, as the dial value changes from 0 to 10000, your lcd will display numbers in range 0.00 to 100.00, accordingly.