Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A custom push button based widget

A custom push button based widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 416 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    viniltc
    wrote on last edited by viniltc
    #1

    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

    jsulmJ Pl45m4P 2 Replies Last reply
    0
    • V viniltc

      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

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      3
      • V viniltc

        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

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @viniltc

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


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        3
        • jsulmJ jsulm

          @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.

          V Offline
          V Offline
          viniltc
          wrote on last edited by viniltc
          #4

          @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

          jsulmJ 1 Reply Last reply
          0
          • V viniltc

            @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

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @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.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved