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

How to use QPushButton clicked(), pressed() and released() events



  • I want to implement clicked, pressed, released events for a QPushButton to send messages to another program.

    If user clicks once, the application sends click_message. If user keeps pressing on the button, the application sends press_message. If user releases the button, the application sends release_message.

    However, when I implement all of the three events, the application seems to recognize only pressed() and release() events. How can the application also recognize the clicked() event?

    More specifically, I use the button to control a motor. The click_message commands the motor to run 100 steps. The press_message commands the motor to run continuously until it receives release_message.



  • @phnhung98
    Show your code, or better try a standalone example. QPushButton::clicked works fine, so we can't tell what your issue is.

    BTW, don't forget you are going to get multiple events here. I would expect when user clicks mouse that not only do you get clicked signal but also you will get pressed & released signals for the mouse click. You will have to deal with that in your code logic, as it sounds like you will not want to act on pressed instantaneously in case it is going to be the start of clicked.



  • @JonB Thank you for your answer. Here are the slots of clicked, pressed and released events
    void Form::on_btn_move_up_clicked()
    {
    txdata[0]=0x0B;
    txdata[1]=0x03;
    txdata[2]=0x64; // move 100 steps
    emit(MoveStageSignal(txdata));

    }

    void Form::on_btn_move_up_pressed()
    {
    txdata[0]=0x0B;
    txdata[1]=0x03;
    txdata[2]=0x02; //run continuously
    emit(MoveStageSignal(txdata));
    }

    void Form::on_btn_move_up_released()
    {
    txdata[0]=0x0B;
    txdata[1]=0x03;
    txdata[2]=0x01; //hard stop
    emit(MoveStageSignal(txdata));
    }


  • Lifetime Qt Champion

    @phnhung98 said in How to use QPushButton clicked(), pressed() and released() events:

    on_btn_move_up_clicked()

    Can you show the connect() call where you connect this slot to clicked() signal?



  • @jsulm The connect() and the slots are autogenerated by "go to slot" in Qt Designer, so I do not have the connect() in my code.


  • Lifetime Qt Champion

    @phnhung98 Ah, just realised you're using the auto-connect feature. This feature is known for being error prone, it is safer to do the connections manually in code.


Log in to reply