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

QPushbutton pressed



  • Hi guys , i want to move the circle with 4 Qpushbutton while they are pressed ( not only click ) , i tried signal pressed but didn't work (it work like simple click ) , i have ready in some forum that i have to use event , i tried this function but it didn't work ( i understand the phenomen but i don't know how to write it 0_1549960214300_Capture d’écran 2019-02-11 à 14.12.45.png 0_1549960223372_Capture d’écran 2019-02-11 à 16.39.10.png
    P.S: BTN_Up is the bouton up (QpushButton)
    Thanks


  • Moderators

    hi @Zunneh

    I fairly certain you'll need to use a QTimer here to regularly update your label position, while the button is pressed.

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent) {
        QTimer *timer = new QTimer(this);
        
        timer->setIntervall(1000) //1000ms
    
        connect(timer, &QTimer::timeout, this, &MainWindow:: updatePosition);
    
        connect(ui-> BTN_Up, &QPushButton::pressed, timer, QOverload<void>::of(&QTimer::start));
    connect(ui-> BTN_Up, &QPushButton::released, timer, &QTimer::stop);
    
    
    }
    
    
    void MainWindow::updatePosition() {
        if(ui->BTN_Up->isDown()) {
           currentY ++;
       } else {.....}
    
       ui->label->move(currentX, currentY);
    }
    

    untested Code, so no guaranties for typos and stuff ;-)



  • @Zunneh

    like this:

    ui-> BTN_Up->setAutoRepeat(true);
    connect(ui->BTN_Up, &QPushButton::pressed, []{
            qDebug() << "Pressed!";
    });
    

    also use:
    autoRepeatDelay() and setAutoRepeatInterval()



  • @Devopia53 Thank you guy you saved me :)
    @J-Hilk i tried @Devopia53 solution because it was easy and work perfect , i will try your solution , thank you guys


  • Moderators

    @Devopia53 I total forgot autorepeat was a thing! thumbs up

    @Zunneh if your question is answered, don't forget to set the topic to solved, using the topic tools.


Log in to reply