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.5k 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 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.

    JonBJ 1 Reply Last reply
    0
    • P phnhung98

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      2
      • JonBJ JonB

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

        jsulmJ 1 Reply Last reply
        0
        • P phnhung98

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          0
          • jsulmJ jsulm

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

            jsulmJ 1 Reply Last reply
            0
            • P phnhung98

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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

              • Login

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