PDA

View Full Version : Telling apart single click and double click in QTableView



jgirlich
26th February 2013, 15:43
Hi,

I'm using a QTableView (cannot change to QTableWidget) and need a Delegate (not an editor) to do different actions on single and double mouse clicks. The basic idea I came up with is to start a QTimer on a QEvent::MouseButtonRelease and either stop it on receiving a QEvent::MouseButtonDblClick and depending on the program state maybe open a new QWindow or if no double click event is received to change something on the delegate.

The other QWindow is started through the doubleClicked signal in a controller class:


connect(m_table, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(maybeOpenQWindow(QModelIndex)));


This is my Delegate's event handling:



bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if (QEvent::MouseButtonRelease)
{
qDebug() << "single click";
m_timer->start();
event->accept();
return true;
}
else if (event->type() == QEvent::MouseButtonDblClick)
{
qDebug() << "double click";
m_timer->stop();
event->accept();
return true;
}
else
{
return false;
}
}


I saw in the source code, that QAbstractItemView::mouseDoubleClickEvent is sending out a mouse press event like QWidget::mouseDoubleClickEvent so I overloaded it with an empty function in my QTableView and verified it isn't run.

But I get mixed results.

Double click on MyDelegate while it is focused and no new QWindow is started:



single click
double click
single click


Where do I get the excess single click event from?
(in this case my timer is started again and the undesired single click action is executed, but a double click should be ignored if no new QWindow is started)

Double click on MyDelegate while it is focused and a new QWindow is started:



single click
double click


Why do those situations behave differently?

To sum it up:
I need my delegate to
* always react on a single click (i.e. repaint differently)
* either start a new QWindow or ignore a double click, depending on programs state

Any suggestions how I can achieve this? Completely different approaches are welcome as well.

Cheers
jgirlich

jgirlich
5th March 2013, 15:27
Huh, no suggestions? Did I ask my question in a bad way or is there nobody who can explain the Qt event behaviour to me?

Anyway, best solution I found so far: Using a timer to delay the execution of 'maybeOpenQWindow' so the second single click event goes through and I can stop my m_timer on it.



bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if (QEvent::MouseButtonRelease)
{
qDebug() << "single click";
if (m_timer->isActive())
m_timer->stop();
else
m_timer->start();
event->accept();
return true;
}
else
{
return false;
}
}


And the slot looks like this:



void maybeOpenQWindow(const QModelIndex &index)
{
QTimer::singleShot(75, this, SLOT(reallyMaybeOpenQWindow()));
}


I know it's really dirty, but since I don't seem to understand the event inside a QTableView and it's delegates it's the only way I found to get it working.