tags:

views:

364

answers:

2

I need to run slot only on doubleClick with left button mouse, instead of both.

connect(this -> myComponent, SIGNAL (doubleClicked (const QModelIndex & )), this,
  SLOT (performSomeAction(const QModelIndex & )));

With this event, double click works in both cases, but needed only with left button click. How I can do it?

this -> myComponent => QTableView

A: 

I haven't done Qt for a while but this should work. Since QTableView is a QWidget, you could also re-implement mouseDoubleClickEvent ( QMouseEvent * e ) which would tell you which button was used. Take care of calling the parent implementation. You only want to know which button was used but want to handle the double click using the callback with the model.

So it could look like:

myComponent::mouseDoubleClickEvent( QMouseEvent * e )
{
    m_leftButtonUsed = false;
    if ( e->button() == Qt::LeftButton )
    {
        m_leftButtonUsed = true;
    }

    // This will call doubleClicked (const QModelIndex & )
    QTableView::mouseDoubleClickedEvent(e);
}
Eric Fortin
Nope, it doesn't work for me. I derive my new class inheritance from QTableView, and implement there mouseDoubleClickEvent, but this method doesn't invoke.
Alex Ivasyuv
A: 

I found the following solution solution:

this -> myComponent -> viewport() -> installEventFilter(this);

bool MyClass::eventFilter(QObject *obj, QEvent *event) {
  this -> event = event;
  return QWidget::eventFilter(obj, event);
}

...

if (this -> event -> type() == QEvent::MouseButtonDblClick) {
  QMouseEvent * mouseEvent = static_cast <QMouseEvent *> (event);

  if (mouseEvent -> button() == Qt::LeftButton) {
    // do something....
  }
}
Alex Ivasyuv