How can i set buttonbox 3 second pressing?



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



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



  • 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 ?



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


  • @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!");
        }
    }
    


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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.