PDA

View Full Version : How to get mouse press events out from a QTableWidget?



Ran
2nd April 2008, 13:17
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);
}

wysota
2nd April 2008, 13:40
Install the filter on the table's viewport widget and not the table itself.

Ran
2nd April 2008, 14:19
Good point! wysota! It solved my question easily. Thanks a lot