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. QTableView click and double click signals
Forum Updated to NodeBB v4.3 + New Features

QTableView click and double click signals

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 5.3k Views 2 Watching
  • 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.
  • SPlattenS SPlatten

    I have a QTableView, I've connected to both click and doubleclick, different purposes and different slots.

    click on an item and the slot gets the ID of the item clicked and enables other widgets.
    doubleclick on an item and the slot gets the data for the row and loads into the dialog for editing.

    What I've noticed is the a doubleclick also causes two invocations of click, the sequence in the Application Output is:

    click
    doubleclick
    click
    

    Is there anyway to prevent this from happening ?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @SPlatten said in QTableView click and double click signals:

    Is there anyway to prevent this from happening ?

    No, that's what Qt sees: a click initially, a doubleclick when it gets the second click within a certain period of time.

    If you need to distinguish you will need to set a time on the first click received, and see whether you get the doubleclick within a certain period, before the timer expires. If the timer expires without doubleclick, you then act on the original click only. I think see e.g. https://forum.qt.io/topic/54723/solved-handle-mouse-double-clicked-and-mouse-single-click-event/11

    SPlattenS 2 Replies Last reply
    4
    • JonBJ JonB

      @SPlatten said in QTableView click and double click signals:

      Is there anyway to prevent this from happening ?

      No, that's what Qt sees: a click initially, a doubleclick when it gets the second click within a certain period of time.

      If you need to distinguish you will need to set a time on the first click received, and see whether you get the doubleclick within a certain period, before the timer expires. If the timer expires without doubleclick, you then act on the original click only. I think see e.g. https://forum.qt.io/topic/54723/solved-handle-mouse-double-clicked-and-mouse-single-click-event/11

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by SPlatten
      #3

      @JonB , thank you, I'm implementing a timer to manage the click and double clicks.

      When the initial click is picked up, it starts a 50ms one shot timer, if this expires then it is a single click and the timer slot is lambda so it performs the normal single click functionality.

      If a double click signal occurs then it stops the one shot timer.

      [Edit] 50 ms isn't enough or Windows is just rubbish and cannot time to that resolution, its Windows 10...changed to 100ms.

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • JonBJ JonB

        @SPlatten said in QTableView click and double click signals:

        Is there anyway to prevent this from happening ?

        No, that's what Qt sees: a click initially, a doubleclick when it gets the second click within a certain period of time.

        If you need to distinguish you will need to set a time on the first click received, and see whether you get the doubleclick within a certain period, before the timer expires. If the timer expires without doubleclick, you then act on the original click only. I think see e.g. https://forum.qt.io/topic/54723/solved-handle-mouse-double-clicked-and-mouse-single-click-event/11

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #4

        @JonB , what is going on?

        In my lambda click handler:

        mstkConnections.push(QOject::connect(mptvSDrecs,
                                             &QTableView::clicked,
            [this]() {
            //Until we know otherwise assume this is a single click
                mblnSingleClick = true;
                QTimer::singleShot(DataSets::msuint16MouseClickManager,  [this]() {
                    if ( mblnSingleClick != true ) {
                        return;
                    }
        qDebug() << "SingleClick!";
        ...
                });
            }
        ));
        mstkConnections.push(QOject::connect(mptvSDrecs,
                                             &QTableView::doubleClicked,
            [this]() {
            //Prevent mange click management timer from doing the click
                mblnSingleClick = false;
        qDebug() << "DoubleClick!";
        ...
                });
            }
        ));
        

        In the Application Output, when a single click on it's on just:

        SingleClick!
        

        When a double click:

        DoubleClick
        SingleClick!
        

        Why? The flag is set to false, when the timer expires it should see the flag is false and exit, but it isn't.

        [Edit] I've just modified the output to show the state of the variable mblnSingleClick, now I see:

        DoubleClick false
        SingleClick! true
        

        Is this something weird about the way Lambda slots work because the member variable isn't being reset to true again, yet the slot that is called after the doubleClick still sees it as true?
        [Another Edit] Nope, nothing to do with Lambda have moved the slot into a standard C++ method, exactly the same thing is happening.

        Kind Regards,
        Sy

        mrjjM 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @JonB , what is going on?

          In my lambda click handler:

          mstkConnections.push(QOject::connect(mptvSDrecs,
                                               &QTableView::clicked,
              [this]() {
              //Until we know otherwise assume this is a single click
                  mblnSingleClick = true;
                  QTimer::singleShot(DataSets::msuint16MouseClickManager,  [this]() {
                      if ( mblnSingleClick != true ) {
                          return;
                      }
          qDebug() << "SingleClick!";
          ...
                  });
              }
          ));
          mstkConnections.push(QOject::connect(mptvSDrecs,
                                               &QTableView::doubleClicked,
              [this]() {
              //Prevent mange click management timer from doing the click
                  mblnSingleClick = false;
          qDebug() << "DoubleClick!";
          ...
                  });
              }
          ));
          

          In the Application Output, when a single click on it's on just:

          SingleClick!
          

          When a double click:

          DoubleClick
          SingleClick!
          

          Why? The flag is set to false, when the timer expires it should see the flag is false and exit, but it isn't.

          [Edit] I've just modified the output to show the state of the variable mblnSingleClick, now I see:

          DoubleClick false
          SingleClick! true
          

          Is this something weird about the way Lambda slots work because the member variable isn't being reset to true again, yet the slot that is called after the doubleClick still sees it as true?
          [Another Edit] Nope, nothing to do with Lambda have moved the slot into a standard C++ method, exactly the same thing is happening.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @SPlatten

          Hi
          Lambas uses "captures" that allows you to keep a copy of variables.
          You seem to capture "this" i thought that the members would be "live" ?

          Can you try directly capture the flag by reference?

          Timer::singleShot(DataSets::msuint16MouseClickManager, [&mblnSingleClick]

          (both places)

          and see if it sort of still has its start value ?

          SPlattenS 1 Reply Last reply
          0
          • mrjjM mrjj

            @SPlatten

            Hi
            Lambas uses "captures" that allows you to keep a copy of variables.
            You seem to capture "this" i thought that the members would be "live" ?

            Can you try directly capture the flag by reference?

            Timer::singleShot(DataSets::msuint16MouseClickManager, [&mblnSingleClick]

            (both places)

            and see if it sort of still has its start value ?

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #6

            @mrjj , not sure I follow, I'm passing this to the single shot slot, but now the slot is set-up with:

            QTimer::singleShot(DataSets::mscuint16MouseClickManager, this,
                              &DataSets::singleClick);
            

            Behaviour is still the same.

            Kind Regards,
            Sy

            mrjjM 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @mrjj , not sure I follow, I'm passing this to the single shot slot, but now the slot is set-up with:

              QTimer::singleShot(DataSets::mscuint16MouseClickManager, this,
                                &DataSets::singleClick);
              

              Behaviour is still the same.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #7

              @SPlatten

              Hi
              Im talking about
              https://www.learncpp.com/cpp-tutorial/lambda-captures/

              It sounds like it has copy of the flag since it dont change.

              Ah never mind. just realized you got rid of the lambdas :)

              So if timer is using a std slot it cant be that.

              If you inspect the value in the debugger. does it change ?

              SPlattenS 3 Replies Last reply
              0
              • mrjjM mrjj

                @SPlatten

                Hi
                Im talking about
                https://www.learncpp.com/cpp-tutorial/lambda-captures/

                It sounds like it has copy of the flag since it dont change.

                Ah never mind. just realized you got rid of the lambdas :)

                So if timer is using a std slot it cant be that.

                If you inspect the value in the debugger. does it change ?

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #8

                @mrjj, I've replaced both slots with C++ slots now, no lambda, the functionality is still the same, I also tried inserting a call to:

                QCoreApplication::processEvents();
                

                No difference. I've checked over and over, there is only one place where I set the boolean flag to true and one place where its set to false, but for some unknown reason setting it to false doesn't effect the state shown in the one shot slot.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • mrjjM mrjj

                  @SPlatten

                  Hi
                  Im talking about
                  https://www.learncpp.com/cpp-tutorial/lambda-captures/

                  It sounds like it has copy of the flag since it dont change.

                  Ah never mind. just realized you got rid of the lambdas :)

                  So if timer is using a std slot it cant be that.

                  If you inspect the value in the debugger. does it change ?

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #9

                  @mrjj , I have set a break point now, will check, but when I set the state to false, the next line echos it to the Application Output and the state is as I set to false, but the next line which comes from the single shot timer is different...will set breakpoint there too.

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @SPlatten

                    Hi
                    Im talking about
                    https://www.learncpp.com/cpp-tutorial/lambda-captures/

                    It sounds like it has copy of the flag since it dont change.

                    Ah never mind. just realized you got rid of the lambdas :)

                    So if timer is using a std slot it cant be that.

                    If you inspect the value in the debugger. does it change ?

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #10

                    @mrjj , found it!

                    I needed a check in the single click slot to recognise that the signal was part of a double click and do nothing, because regardless of the timer, I still need to ignore the second signal.

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      Mickleholts
                      Banned
                      wrote on last edited by Mickleholts
                      #11
                      This post is deleted!
                      1 Reply Last reply
                      1

                      • Login

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