How to get mouse press events out from a QTableWidget which is part of another widget?
I am using event filter and installEventFilter method. But it can only get mousepress event when I click the very edge of the QTableWidget object.
The interesting thing is that the key-pressing events and wheel events are always caught by the code.
Anybody knows how to deal with this? Thanks!
Code is:
// install event filter for tablewidget
tableWidget->installEventFilter(this);
bool AotfFilterGui
::eventFilter( QObject *object,
QEvent *event
) {
// firstly, check whether the object is the QTableWidget and if it's a mouse press event
if (object == tableWidget)
if( event
->type
() == QEvent::MouseButtonPress) { // if yes, we need to cast the event into a QMouseEvent type
QMouseEvent *pMouseEvent
= static_cast<QMouseEvent
*>
(event
);
// check whether it's mid button pressed
/*if (pMouseEvent->button() == Qt::MidButton)
{*/
//do the processing and return true
textBrowserErrorInfo->setText(tr("clicked"));
return true;
//}
}
else if (event
->type
() == QEvent::KeyPress) { QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
textBrowserErrorInfo->setText(tr( "Ate key press: " + keyEvent->text()));
return true;
}
return QWidget::eventFilter(object, event
);
}
// install event filter for tablewidget
tableWidget->installEventFilter(this);
bool AotfFilterGui::eventFilter( QObject *object, QEvent *event )
{
// firstly, check whether the object is the QTableWidget and if it's a mouse press event
if (object == tableWidget)
if( event->type() == QEvent::MouseButtonPress)
{ // if yes, we need to cast the event into a QMouseEvent type
QMouseEvent *pMouseEvent = static_cast<QMouseEvent *>(event);
// check whether it's mid button pressed
/*if (pMouseEvent->button() == Qt::MidButton)
{*/
//do the processing and return true
textBrowserErrorInfo->setText(tr("clicked"));
return true;
//}
}
else if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
textBrowserErrorInfo->setText(tr( "Ate key press: " + keyEvent->text()));
return true;
}
return QWidget::eventFilter(object, event);
}
To copy to clipboard, switch view to plain text mode
Bookmarks