Qt double click check left button mouse
I need to run slot only on doubleClick with left button mouse, instead of both.
// this->myComponent is a QTableView
connect(this -> myComponent, SIGNAL (doubleClicked (const QModelIndex & )), this,
SLOT (performSomeAction(const QModelIndex & )));
开发者_如何学GoWith this event, double click works in both cases, but needed only with left button click. How I can do it?
this -> myComponent => QTableView
I found the following 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....
}
}
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);
}
doubleClicked signal is emitted from the mouseDoubleClick slot. You could override the mouseDoubleClick slot checking if the double click was from the left button, if the double click was left then you can call the mouseDoubleClick parent method and then the doubleClicked signal would be emitted.
@pyqtSlot(QMouseEvent)
def mouseDoubleClickEvent(self, event:QMouseEvent):
if event.button() == Qt.LeftButton:
super().mouseDoubleClickEvent(event)
精彩评论