QTableView click and double click signals
-
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 ?
-
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 ?
@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
-
@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
@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.
-
@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
@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. -
@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.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 ?
-
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 ?
-
@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.
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 ?
-
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 ?
@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.
-
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 ?
@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.
-
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 ?
-
This post is deleted!