Distinguish Between Single and Double Click Events in Qt

Distinguish between single and double click events in Qt

You can find answer in the thread titled Double Click Capturing on QtCentre forum;

You could have a timer. Start the
timer in the releaseEvent handler and
make sure the timeout is long enough
to handle the double click first.
Then, in the double click event
handler you can stop the timer and
prevent it from firing. If a double
click handler is not triggered, the
timer will timeout and call a slot of
your choice, where you can handle the
single click. This is of course a
nasty hack, but has a chance to work.

wysota

How to distinguish between mouseReleaseEvent and mousedoubleClickEvent on QGraphicsScene

For the logic that you want to occur on a double click, put the code inside mouseDoubleClickEvent() and for the logic that you want to occur on a mouse release, put the code inside mouseReleaseEvent().

If you want to do something when the user clicks but doesn't double click, you have to wait to see if they click twice or not. On the first mouse release start a 200ms timer.

If you get a mouseDoubleClickEvent() before the timer expires then it was a double click and you can do the double click logic. If the timer expires before you get another mouseDoubleClick() then you know it was a single click.

Pseudocode

main()
{
connect(timer, SIGNAL(timeout()), this, SLOT(singleClick()));
}

mouseReleaseEvent()
{
timer->start();
}

mouseDoubleClickEvent()
{
timer->stop();
}

singleClick()
{
// Do single click behavior
}

This answer gives a rather similar solution.

PyQt5: Checkbox that distinguishes between one and double clicks?

First, you may want to ask if this is what you really want. The problem is that checkboxes are a well-recognized UI element that's practically always single click, so you are violating your users' expectations if you create a double-click checkbox.

EDIT: if you decide you want it, you might try the code below. The idea is to subclass QCheckBox and implement new handlers for click and double click, where double click always sets PartiallyChecked (the square) and single click cycles between Checked and Unchecked.

import sys
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QCheckBox, QApplication
from PyQt5 import QtCore

class DblClickCheckBox(QCheckBox):
def mouseDoubleClickEvent(self, event):
self.setCheckState(QtCore.Qt.PartiallyChecked)

def mousePressEvent(self, event):
desired_state = QtCore.Qt.Unchecked if self.checkState() else QtCore.Qt.Checked
self.setCheckState(desired_state)

class Example(QDialog):
def __init__(self):
super().__init__()
btn = DblClickCheckBox('Our special checkbox')
btn.setTristate() # needed to have a 3-state checkbox
lo = QVBoxLayout()
lo.addWidget(btn)
self.setLayout(lo)
self.show()

if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
app.exec_()

Is it possible to suppress single-click events during a double-click?

Start a timer when you get WM_LBUTTONDOWN (or Qt equivalent). If you get WM_LBUTTONDBLCLK (or Qt equivalent) before the timer expires, cancel the timer and execute your double-click action. Otherwise, when the timer expires, execute your single-click event.

On Windows, you can get the double-click time using GetDoubleClickTime().

That's about the best you can do - you can't prevent the single click message being generated in the first place on either platform.

How to recognize the type of a click on a QWidget

I met the same problem before. I think it is by design of Qt. This may not be a best solution, but what you can do is to create an event filter and 'wait' for a while:

bool m_bClicked = false;

bool eventFilter(QObject* object, QEvent* event)
{
if(event->type() == QEvent::MouseButtonPress)
{
// Wait for 400 milliseconds
m_bClicked = true;
QTimer::singleShot(400, this, SLOT(eventMousePressTimer()));
return true;
}
else if(event->type() == QEvent::MouseButtonDblClick)
{
// Double-clicked
m_bClicked = false;
return true;
}
return false;
}

void eventMousePressTimer()
{
if(m_bClicked)
{
// Single-clicked
}
}

QPushButton in QGraphicsScene requires doubleClick instead of single click

mousePressEvent in the Qt documentation :

The default implementation depends on the state of the scene. If there
is a mouse grabber item, then the event is sent to the mouse grabber.
Otherwise, it is forwarded to the topmost visible item that accepts
mouse events at the scene position from the event, and that item
promptly becomes the mouse grabber item.

So if you reimplement it like you have done in your code, the event is no longer sent to the mouse grabber (your button) but when you double click, this event is not catched by mousePressEvent (but normally by mouseDoubleClickEvent) and the button is activated only one because the first mouse press is ignored to detect if it is simple click or double click.

hope this will help you.


Update : to sove your problem just change your mouseMoveEvent to :

void mousePressEvent(QGraphicsSceneMouseEvent* event){
qDebug()<<event->scenePos();
QGraphicsScene::mousePressEvent(event);
}

But I advise you to subclass QGraphicsView and to overload his method mousePressEvent.

Hope this could help you.

Confused by doubleclickevent and mousepressevent in Qt

A QGraphicsView is a widget, so it has a doubleClickEvent. In contrast, a QGraphicsItem is not a widget, but has a mousePressEvent.

When you override events, if you don't want them to be propagated to other objects, you need to accept the event to tell the system that you've handled it.

void MyGraphicsView::mouseDoubleClickEvent(QMouseEvent * event)
{
// Create a graphics item..
...
event->accept(); // tell the system that the event has been handled
}

Without accepting the event, the double click is passed on to the QGraphicsScene and then QGraphicsItem.

If you want to be able to double-click to create an item when clicking on an empty area of a scene, but have an item react if you double-click on an item, you can override the scene's mouseDoubleClickEvent instead of the view and check if there's an item at the cursor position before deciding upon whether or not to create an item, or pass the event on.

void QGraphicsScene::mouseDoubleClickEvent( QGraphicsSceneMouseEvent * mouseEvent )
{
if(items(mouseEvent->scenePos()).isEmpty()) // if no items are at this position
{
// create new graphics item
...
mouseEvent->accept(); // don't propagate the event
}

// no items, so allow the event to propagate by doing nothing
}

Handling single click and double click separately in QTableWidget

Okey, now i see. I had similar problem few weeks ago. The problem is in your QTableWidgetItem. I don't know exactly how does it work, but sometimes you can miss your item and click on cell. That's how you can fix it. Connect it this way:

connect(ui.tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(myCellClicked(int, int)));
connect(ui.tableWidget, SIGNAL(cellDoubleClicked(int,int)), this, SLOT(tableItemClicked(int,int)));

And in your tableItemClicked slot do it this way:

void MyWidget::tableItemClicked(int row, int column)
{
QTableWidgetItem *item = new QTableWidgetItem;
item = myTable->item(row,column)
/* do some stuff with item */
}


Related Topics



Leave a reply



Submit