[Interest] Set QTableWidgetItem checkbox when clicking anywhere on QTableWidget's row

Murphy, Sean smurphy at walbro.com
Wed Jul 5 20:16:26 CEST 2017


I've got a QTableWidget that presents data in two columns. The first column is made up of QTableWidgetItems that have the Qt::ItemIsUserCheckable flag enabled. The items in the second column are not checkable. What I'd like to do is allow the user to click anywhere in a given row to toggle that row's checkbox state, so they don't have to only click on the checkbox in column 0 itself.

I've created my own slot, connected the QTableWidget::cellClicked(int row, int column) signal to my slot, and take care of programmatically toggling that row's checkbox. This almost works. It is fine EXCEPT when the user actually clicks on a checkbox. In that case the checkbox takes care of toggling itself first, and then my slot comes along and toggles it back to its original state and the checkbox ultimate remains in its original state.

Is there any way for me to detect that a click actually happened on an item's checkbox so that I can just let the checkable QTableWidgetItem do its thing, and skip executing my custom slot? I'm not seeing anything in the documentation that allows me to differentiate the checkbox's text from the checkbox itself (let alone the white space that is inside the QTableWidgetItem's cell - the cell's internal margin). 

The only thing I can think of at the moment is that I bump my table up to three columns, splitting my current column 0 into columns 0 and 1 where:
 - column 0 contains checkable QTableWidgetItems that have no text
 - column 1 contains non-checkable QTableWidgetItems that have the text that I current have on my items in my current column 0
 - column 2 doesn't change from my current column 1

Then in my slot, I'd just ignore clicks that happen in column 0, and toggle the checkbox if the click happened in columns 1 or 2. I think this would leave a small window of pixels that are inside the column 0 cell, but outside the checkbox where a user could click and nothing would happen...

Is there a solution to this that I'm missing?

Sean



More information about the Interest mailing list