PDA

View Full Version : QTableView Handling MouseEvent



EvanPlett
8th September 2015, 23:00
The Environment:
- OS: Windows 7 64-bit
- QT Version: QT 5.4.1
- Compiler: win64_vc11.0

The Scenario:
I have used Qt Designer to create a QDialog which contains several nested layouts containing multiple QTableViews. I am using CMake to create my Visual Studio project. In this process, CMake "build" the .ui file into the appropriate C++ files.

The Need:
I want to be able to handle a mouse click on one of those tables and know which cell was clicked.

The Problem
When I use "setMouseTracking(true)" on the containing dialog, it does not handle mouse clicks on the previously mentioned QTableView.

My First Solution (and why it doesn't work):
My first thought was to extend QTableView with my own class so that I could override the "mousePressEvent()" function, but the QTableView is "built" by CMake, so I can't just replace it with my own class which extends QTableView or I loose the position and settings configured in QtDesigner.


Does anyone have any idea how I can solve my problem?

anda_skoa
8th September 2015, 23:28
The Need:
I want to be able to handle a mouse click on one of those tables and know which cell was clicked.

A very common need, that's why the view has a signal for that.



My first thought was to extend QTableView with my own class so that I could override the "mousePressEvent()" function, but the QTableView is "built" by CMake, so I can't just replace it with my own class which extends QTableView or I loose the position and settings configured in QtDesigner.

If you want to do it that way, use the "promote widget" functionality to promote the tableview in design to your class.

Cheers,
_