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. How can i set buttonbox 3 second pressing?
Forum Updated to NodeBB v4.3 + New Features

How can i set buttonbox 3 second pressing?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.7k Views 1 Watching
  • 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.
  • lordumuzcaL Offline
    lordumuzcaL Offline
    lordumuzca
    wrote on last edited by
    #1

    How can i set buttonbox 3 second pressing? For example i try to change label. But i need 3second pressing the button.

    1 Reply Last reply
    0
    • yeckelY Offline
      yeckelY Offline
      yeckel
      wrote on last edited by
      #2

      You have to implement your own onMousePressEvent() and onMouseReleaseEvent() with starting QTimer.

      1 Reply Last reply
      4
      • lordumuzcaL Offline
        lordumuzcaL Offline
        lordumuzca
        wrote on last edited by
        #3

        My code:

        in header:

        private:
            Ui::MainWindow *ui;
            QTimer *counter;
        
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QTimer>
        
        
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {    
            ui->setupUi(this);
            QTimer *counter = new QTimer(this);
            connect(counter,SIGNAL(timeout()),this,SLOT(update());
        }
        
        MainWindow::~MainWindow()
        {
        
            delete ui;
        }
        
        void MainWindow::on_pushButton_pressed()
        {
            counter->start(1000);
        }
        
        void MainWindow::on_pushButton_released()
        {
            counter->stop();
            if(counter>=3000){
                ui->label->setText("ggwp");
            }
        }
        
        

        I guess counter is not used like that. How can i use ?

        J.HilkJ D 2 Replies Last reply
        0
        • lordumuzcaL lordumuzca

          My code:

          in header:

          private:
              Ui::MainWindow *ui;
              QTimer *counter;
          
          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QTimer>
          
          
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {    
              ui->setupUi(this);
              QTimer *counter = new QTimer(this);
              connect(counter,SIGNAL(timeout()),this,SLOT(update());
          }
          
          MainWindow::~MainWindow()
          {
          
              delete ui;
          }
          
          void MainWindow::on_pushButton_pressed()
          {
              counter->start(1000);
          }
          
          void MainWindow::on_pushButton_released()
          {
              counter->stop();
              if(counter>=3000){
                  ui->label->setText("ggwp");
              }
          }
          
          

          I guess counter is not used like that. How can i use ?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @lordumuzca

          You're welcome.

          #ifndef CBUTTON_H
          #define CBUTTON_H
          
          #include <QPushButton>
          #include <QTimer>
          
          class cButton : public QPushButton
          {
              Q_OBJECT
          public:
              explicit cButton(QWidget * parent = 0) :  QPushButton(parent){
              cTimer.setSingleShot(true);
              connect(&cTimer, &QTimer::timeout, this, &cButton:longPressed);
          }
              void setTimeout(int mSec){mSeconds =mSec;}
          signals:
              void longPressed();
          
          protected:
              virtual void mousePressEvent(QMouseEvent *e)Q_DECL_OVERRIDE{
                  QPushButton::mousePressEvent(e);
                  cTimer.start(mSeconds);
              }
          
              virtual void mouseReleaseEvent(QMouseEvent *e)Q_DECL_OVERRIDE{
                  QPushButton::mouseReleaseEvent(e);
                  cTimer.stop();
              }
          
          private:
              QTimer cTimer;
              int mSeconds = 3000; 
          }
          #endif // CBUTTON_H
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • lordumuzcaL lordumuzca

            My code:

            in header:

            private:
                Ui::MainWindow *ui;
                QTimer *counter;
            
            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QTimer>
            
            
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {    
                ui->setupUi(this);
                QTimer *counter = new QTimer(this);
                connect(counter,SIGNAL(timeout()),this,SLOT(update());
            }
            
            MainWindow::~MainWindow()
            {
            
                delete ui;
            }
            
            void MainWindow::on_pushButton_pressed()
            {
                counter->start(1000);
            }
            
            void MainWindow::on_pushButton_released()
            {
                counter->stop();
                if(counter>=3000){
                    ui->label->setText("ggwp");
                }
            }
            
            

            I guess counter is not used like that. How can i use ?

            D Offline
            D Offline
            Devopia53
            wrote on last edited by Devopia53
            #5

            @lordumuzca

            If you make code in your style...

            in header:

            #include <QTimer>
            [...]
            private:
                Ui::MainWindow *ui;
                QTimer counter;
            [...]
            

            in cpp:

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {    
                ui->setupUi(this);
            
                counter.setSingleShot(true);
                connect(&counter, &QTimer::timeout, [=]{
                    ui->label->setText("ggwp");
                });
            
            }
            
            void MainWindow::on_pushButton_pressed()
            {
                ui->label->setText("");
                if (counter.isActive())
                    counter.stop();
                counter.start(3000);
            }
            
            void MainWindow::on_pushButton_released()
            {
                if (counter.isActive()) {
                    counter.stop();
                    ui->label->setText("Opps!");
                }
            }
            
            lordumuzcaL 1 Reply Last reply
            1
            • D Devopia53

              @lordumuzca

              If you make code in your style...

              in header:

              #include <QTimer>
              [...]
              private:
                  Ui::MainWindow *ui;
                  QTimer counter;
              [...]
              

              in cpp:

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {    
                  ui->setupUi(this);
              
                  counter.setSingleShot(true);
                  connect(&counter, &QTimer::timeout, [=]{
                      ui->label->setText("ggwp");
                  });
              
              }
              
              void MainWindow::on_pushButton_pressed()
              {
                  ui->label->setText("");
                  if (counter.isActive())
                      counter.stop();
                  counter.start(3000);
              }
              
              void MainWindow::on_pushButton_released()
              {
                  if (counter.isActive()) {
                      counter.stop();
                      ui->label->setText("Opps!");
                  }
              }
              
              lordumuzcaL Offline
              lordumuzcaL Offline
              lordumuzca
              wrote on last edited by
              #6

              @Devopia53 said in How can i set buttonbox 3 second pressing?:

              @lordumuzca

              If you make code in your style...

              in header:

              #include <QTimer>
              [...]
              private:
                  Ui::MainWindow *ui;
                  QTimer counter;
              [...]
              

              in cpp:

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {    
                  ui->setupUi(this);
              
                  counter.setSingleShot(true);
                  connect(&counter, &QTimer::timeout, [=]{
                      ui->label->setText("ggwp");
                  });
              
              }
              
              void MainWindow::on_pushButton_pressed()
              {
                  ui->label->setText("");
                  if (counter.isActive())
                      counter.stop();
                  counter.start(3000);
              }
              
              void MainWindow::on_pushButton_released()
              {
                  if (counter.isActive()) {
                      counter.stop();
                      ui->label->setText("Opps!");
                  }
              }
              

              It works. I think my style is very easy for understanding. I try to make somethings but i can't find any resources. So i use here. Thank you.

              D 1 Reply Last reply
              1
              • lordumuzcaL lordumuzca

                @Devopia53 said in How can i set buttonbox 3 second pressing?:

                @lordumuzca

                If you make code in your style...

                in header:

                #include <QTimer>
                [...]
                private:
                    Ui::MainWindow *ui;
                    QTimer counter;
                [...]
                

                in cpp:

                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {    
                    ui->setupUi(this);
                
                    counter.setSingleShot(true);
                    connect(&counter, &QTimer::timeout, [=]{
                        ui->label->setText("ggwp");
                    });
                
                }
                
                void MainWindow::on_pushButton_pressed()
                {
                    ui->label->setText("");
                    if (counter.isActive())
                        counter.stop();
                    counter.start(3000);
                }
                
                void MainWindow::on_pushButton_released()
                {
                    if (counter.isActive()) {
                        counter.stop();
                        ui->label->setText("Opps!");
                    }
                }
                

                It works. I think my style is very easy for understanding. I try to make somethings but i can't find any resources. So i use here. Thank you.

                D Offline
                D Offline
                Devopia53
                wrote on last edited by
                #7

                @lordumuzca

                Welcome~
                I agree with you. However, I think the method proposed by @J-Hilk is better depending on the concept of code reusability and OOP.

                1 Reply Last reply
                0

                • Login

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