Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

A custom push button based widget



  • Hi friends,

    I need to use widget which looks like this
    finecorsebutton.JPG

    Which is basically a "push button based fine/coarse " adjustment buttons. Pressing +/- change value by one step and pressing ++/-- change value in multiple steps.

    the implementation is given below:

    adjustmentbutton.h

    #ifndef ADJUSTMENTBUTTON_H
    #define ADJUSTMENTBUTTON_H
    
    #include <QWidget>
    
    class QTimer;
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class AdjustmentButton; }
    QT_END_NAMESPACE
    
    class AdjustmentButton : public QWidget
    {
        Q_OBJECT
    
    public:
        AdjustmentButton(QWidget *parent = nullptr);
        ~AdjustmentButton();
    
    protected 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();
    
    private:
        Ui::AdjustmentButton *ui;
        QTimer *timer_high, *timer_low;
        int timerTimeout;
        int number;
    };
    #endif // ADJUSTMENTBUTTON_H
    

    and adjustmentbutton.cpp

    #include "adjustmentbutton.h"
    #include "ui_adjustmentbutton.h"
    #include <QTimer>
    
    AdjustmentButton::AdjustmentButton(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::AdjustmentButton)
    {
        ui->setupUi(this);
    
        number = 0;
        timerTimeout = 0;
        timer_high = new QTimer(this);
        timer_low = new QTimer(this);
    
        connect(timer_high,&QTimer::timeout, this, &AdjustmentButton::doIncrement);
        connect(timer_low, &QTimer::timeout, this, &AdjustmentButton::doDecrement);
        connect(timer_high,&QTimer::timeout, this, &AdjustmentButton::doMoreIncrement);
        connect(timer_low, &QTimer::timeout, this, &AdjustmentButton::doMoreDecrement);
    
    
        connect(ui->pushButton_high, &QPushButton::pressed, this, &AdjustmentButton::buttonPressed_high);
        connect(ui->pushButton_high, &QPushButton::released, this, &AdjustmentButton::buttonReleased_high);
    
        connect(ui->pushButton_moreHigh, &QPushButton::pressed, this, &AdjustmentButton::buttonPressed_moreHigh);
        connect(ui->pushButton_moreHigh, &QPushButton::released, this, &AdjustmentButton::buttonReleased_moreHigh);
    
        connect(ui->pushButton_low, &QPushButton::pressed, this, &AdjustmentButton::buttonPressed_low);
        connect(ui->pushButton_low, &QPushButton::released, this, &AdjustmentButton::buttonReleased_low);
    
        connect(ui->pushButton_moreLow, &QPushButton::pressed, this, &AdjustmentButton::buttonPressed_moreLow);
        connect(ui->pushButton_moreLow, &QPushButton::released, this, &AdjustmentButton::buttonReleased_moreLow);
    }
    
    AdjustmentButton::~AdjustmentButton()
    {
        delete ui;
    }
    
    void AdjustmentButton::buttonPressed_high()
    {
    timerTimeout = 5000;
    doIncrement();
    }
    
    void AdjustmentButton::buttonReleased_high()
    {
    timer_high->stop();
    }
    
    void AdjustmentButton::buttonPressed_moreHigh()
    {
    timerTimeout = 5000;
    doMoreIncrement();
    }
    
    void AdjustmentButton::buttonReleased_moreHigh()
    {
    timer_high->stop();
    }
    
    void AdjustmentButton::buttonPressed_low()
    {
    timerTimeout = 5000;
    doDecrement();
    }
    
    void AdjustmentButton::buttonReleased_low()
    {
    timer_low->stop();
    }
    void AdjustmentButton::buttonPressed_moreLow()
    {
    timerTimeout = 5000;
    doMoreDecrement();
    }
    
    void AdjustmentButton::buttonReleased_moreLow()
    {
    timer_low->stop();
    }
    
    void AdjustmentButton::doIncrement()
    {
    ++number;
    ui->label->setText(QString("Value: %1").arg(number));
    if(timerTimeout > 50)
    timerTimeout = timerTimeout / 2;
    timer_high->start(timerTimeout);
    }
    
    void AdjustmentButton::doDecrement()
    
    {
    number--;
    ui->label->setText(QString("Value: %1").arg(number));
    if(timerTimeout > 50)
    timerTimeout = timerTimeout / 2;
    timer_low->start(timerTimeout);
    }
    
    void AdjustmentButton::doMoreIncrement()
    {
    number=number+5;
    ui->label->setText(QString("Value: %1").arg(number));
    if(timerTimeout > 50)
    timerTimeout = timerTimeout / 2;
    timer_high->start(timerTimeout);
    }
    
    void AdjustmentButton::doMoreDecrement()
    
    {
    number=number-5;
    ui->label->setText(QString("Value: %1").arg(number));
    if(timerTimeout > 50)
    timerTimeout = timerTimeout / 2;
    timer_low->start(timerTimeout);
    }
    
    1. This is working fine but don't know if this is the best way to implement those kinds of widget.
    2. I need to reuse this widget in my project (a stack of above-mentioned buttons). In those scenario how can I "reuse" this button mutliple times?

    Thanks in advance


  • Lifetime Qt Champion

    @viniltc said in A custom push button based widget:

    how can I "reuse" this button mutliple times

    Create a new form and put these buttons there. Then you can create as many instances of that form as you like.



  • @viniltc

    Promote it or import it as custom Widget to your QtDesigner.



  • @jsulm @Pl45m4

    I created a new QMainWindow and put a few widget containers and promoted them to the above mentioned widget. Which looks like this:

    window2.JPG

    But what if I need to modify slots in those each widgets.

    For ex. I have defined my own slots in adjustmentbutton.cpp:

    void doIncrement();
    void doDecrement();
    void doMoreIncrement();
    void doMoreDecrement(); 
    

    which needs to be slightly different for promoted widgets


  • Lifetime Qt Champion

    @viniltc said in A custom push button based widget:

    which needs to be slightly different for promoted widgets

    Why do you have slots in these buttonts? Buttons usually emit signals and the receivers decide what to do with these signals.


Log in to reply