Виджет кнопки регулировки нескольких шагов - PullRequest
0 голосов
/ 29 января 2020

Я пытаюсь реализовать кнопку точной / грубой настройки, которая выглядит следующим образом:

Кнопки с одной стрелкой предназначены для точной настройки (маленькая шаги) и двойные кнопки со стрелками предназначены для грубой регулировки (большие шаги). Это класс виджетов, поэтому я могу использовать его несколько раз в своей программе. Реализация выглядит следующим образом:

CurrentButtonOne::CurrentButtonOne(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CurrentButtonOne)
{
    ui->setupUi(this);


    ui->label_currentValue->setText(QString("%1").arg(value));
    ui->label_currentValue->setAlignment(Qt::AlignCenter);

    timerTimeout = 0;
    timer_high = new QTimer(this);
    timer_low = new QTimer(this);



    connect(timer_high,&QTimer::timeout, this, &CurrentButtonOne::doIncrement);
    connect(timer_low, &QTimer::timeout, this, &CurrentButtonOne::doDecrement);
    connect(timer_high,&QTimer::timeout, this, &CurrentButtonOne::doMoreIncrement);
    connect(timer_low, &QTimer::timeout, this, &CurrentButtonOne::doMoreDecrement);


    connect(ui->pushButton_high, &QPushButton::pressed, this, &CurrentButtonOne::buttonPressed_high);
    connect(ui->pushButton_high, &QPushButton::released, this, &CurrentButtonOne::buttonReleased_high);

    connect(ui->pushButton_moreHigh, &QPushButton::pressed, this, &CurrentButtonOne::buttonPressed_moreHigh);
    connect(ui->pushButton_moreHigh, &QPushButton::released, this, &CurrentButtonOne::buttonReleased_moreHigh);

    connect(ui->pushButton_low, &QPushButton::pressed, this, &CurrentButtonOne::buttonPressed_low);
    connect(ui->pushButton_low, &QPushButton::released, this, &CurrentButtonOne::buttonReleased_low);

    connect(ui->pushButton_moreLow, &QPushButton::pressed, this, &CurrentButtonOne::buttonPressed_moreLow);
    connect(ui->pushButton_moreLow, &QPushButton::released, this, &CurrentButtonOne::buttonReleased_moreLow);


}

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

void CurrentButtonOne::buttonPressed_high() 
{
timerTimeout = 5000;
doIncrement(); // single arrow high (fine adjustment)
}

void CurrentButtonOne::buttonReleased_high()
{
timer_high->stop();
}

void CurrentButtonOne::buttonPressed_moreHigh() 
{
timerTimeout = 5000;
doMoreIncrement(); // double arrow high (coarse adjustment) 
}

void CurrentButtonOne::buttonReleased_moreHigh()
{
timer_high->stop();
}

void CurrentButtonOne::buttonPressed_low()
{
timerTimeout = 5000;
doDecrement(); single arrow low(fine adjustment) 
}
}

void CurrentButtonOne::buttonReleased_low()
{
timer_low->stop();
}
void CurrentButtonOne::buttonPressed_moreLow()
{
timerTimeout = 5000;
doMoreDecrement(); double arrow low (coarse adjustment)
}

void CurrentButtonOne::buttonReleased_moreLow()
{
timer_low->stop();
}

void CurrentButtonOne::doIncrement()
{
value=value+500;

ui->label_currentValue->setText(QString("%1").arg(value));
emit getValue(value);

if(timerTimeout > 500)
timerTimeout = timerTimeout / 2;
timer_high->start(timerTimeout);
}

void CurrentButtonOne::doDecrement()

{
value=value-500;

if(value<=0)
   value=0;

ui->label_currentValue->setText(QString("%1").arg(value));
emit getValue(value);
if(timerTimeout > 500)
timerTimeout = timerTimeout / 2;
timer_low->start(timerTimeout);
}

void CurrentButtonOne::doMoreIncrement()
{
value=value+5000;

ui->label_currentValue->setText(QString("%1").arg(value));
emit getValue(value);

if(timerTimeout > 500)
timerTimeout = timerTimeout / 2;
timer_high->start(timerTimeout);
}

void CurrentButtonOne::doMoreDecrement()

{

value=value-5000;

if(value<=0)
   value=0;

ui->label_currentValue->setText(QString("%1").arg(value));

emit getValue(value);
if(timerTimeout > 500)
timerTimeout = timerTimeout / 2;
timer_low->start(timerTimeout);
}

, а файл .h:

#include <QWidget>

class Timer;

namespace Ui {
class CurrentButtonOne;
}

class CurrentButtonOne : public QWidget
{
    Q_OBJECT

public:
    explicit CurrentButtonOne(QWidget *parent = nullptr);
    ~CurrentButtonOne();

public slots:

    void buttonPressed_high();
    void buttonReleased_high();
    void buttonPressed_moreHigh();
    void buttonReleased_moreHigh();
    void buttonPressed_low();
    void buttonReleased_low();
    void buttonPressed_moreLow();
    void buttonReleased_moreLow();
    void doIncrement();
    void doDecrement();
    void doMoreIncrement();
    void doMoreDecrement();

signals:
    void getValue(unsigned int);


private:
    Ui::CurrentButtonOne *ui;
    QTimer *timer_high, *timer_low;
    int timerTimeout;
     unsigned int value = 0 ;
     unsigned int channel_number = 1;
};

Проблемы с этой реализацией:

One мое требование - value не должен go ниже 0. Для этого я проверил

if(value<=0)
   value=0;

в doDecrement() и doMoreDecrement() слотах. Но это не работает. Если я нажимаю любую из кнопок Decrement ниже 0, значение внезапно переходит в таинственное число 4294897292728.

Я немного застрял, чтобы найти проблему. Любые отзывы будут оценены.

Заранее спасибо.

...