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)));
To copy to clipboard, switch view to plain text mode
This is my Delegate's event handling:
{
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;
}
}
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;
}
}
To copy to clipboard, switch view to plain text mode
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
single click
double click
single click
To copy to clipboard, switch view to plain text mode
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
single click
double click
To copy to clipboard, switch view to plain text mode
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
Bookmarks