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 to use QPushButton clicked(), pressed() and released() events
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 6.3k 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.
  • P Offline
    P Offline
    phnhung98
    wrote on 25 Mar 2021, 08:52 last edited by
    #1

    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.

    J 1 Reply Last reply 25 Mar 2021, 08:54
    0
    • P phnhung98
      25 Mar 2021, 08:52

      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.

      J Online
      J Online
      JonB
      wrote on 25 Mar 2021, 08:54 last edited by JonB
      #2

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

      P 1 Reply Last reply 25 Mar 2021, 09:16
      2
      • J JonB
        25 Mar 2021, 08:54

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

        P Offline
        P Offline
        phnhung98
        wrote on 25 Mar 2021, 09:16 last edited by
        #3

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

        J 1 Reply Last reply 25 Mar 2021, 09:18
        0
        • P phnhung98
          25 Mar 2021, 09:16

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

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 25 Mar 2021, 09:18 last edited by
          #4

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

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

          P 1 Reply Last reply 25 Mar 2021, 09:29
          0
          • J jsulm
            25 Mar 2021, 09:18

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

            P Offline
            P Offline
            phnhung98
            wrote on 25 Mar 2021, 09:29 last edited by
            #5

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

            J 1 Reply Last reply 25 Mar 2021, 10:05
            0
            • P phnhung98
              25 Mar 2021, 09:29

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

              J Online
              J Online
              jsulm
              Lifetime Qt Champion
              wrote on 25 Mar 2021, 10:05 last edited by
              #6

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

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

              1 Reply Last reply
              0

              1/6

              25 Mar 2021, 08:52

              • Login

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