Qt4: QTableView mouse button events not caught

I have a QTableView that displays a custom model. I would like to right-click so that I can open the contextual drop-down menu in the following tabular data:

 MainWindow::MainWindow() { QTableView * itsView = new QTableView; itsView->installEventFilter(this); ... //Add other widgets and display them all } bool MainWindow::eventFilter(QObject * watched, QEvent * event) { if(event->type() == QEvent::MouseButtonPress) printf("MouseButtonPress event!\n"); else if(event->type() == QEvent::KeyPress) printf("KeyPress event!\n"); } 

Oddly enough, I get all KeyPress events properly: when I select a cell and press a key, I get a "KeyPress!" Event. message. However, I only get the MouseButtonPress event! when I click on a very thin border around the whole table.

+6
user-interface qt qt4
source share
2 answers

This is because Tableview is a thin border ... If you want to access the contents of the widget, you should set your eventFilter in the Tableview viewport window instead !

Therefore, I suggest:

 QTableView * itsView = new QTableView; itsView->viewport()->installEventFilter(this); 

Try this, it should fix your problem!

Hope this helps!

+10
source share

If you need to show a context menu, you can use the customContextMenuRequested tableview signal; you will need to set the context menu policy to Qt::CustomContextMenu to trigger this signal. Something like that:

 ... itsView->setContextMenuPolicy(Qt::CustomContextMenu); QObject::connect(itsView, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(tableContextPopup(const QPoint &))); ... void MainWindow::tableContextPopup(const QPoint & pos) { qDebug() << "show popup " << pos; } 

Hope this helps, welcome.

+2
source share

All Articles