Solved How To Add Select All Checkbox in QTableWidget QHeaderView
Is there a way to add a "Select All Checkbox" to a QTableWidget in the first header column? I mean, I found that I can add checkboxes on rows beneath the header, just not a checkbox in the header itself.
I tried the following, but it shows no change:
// item(0,0) is a cell with a checkbox in it (first row after the header, first column) QTableWidgetItem *oItem = ui->myTable->item(0,0)->clone(); oItem->setCheckState(Qt::Unchecked); ui->myTable->setHorizontalHeaderItem(0,oItem);
The answer is in the Qt FAQ:
I found that this also worked using an existing QTableWidget that was drawn on the page, like so:
MyHeader *myHeader = new MyHeader(Qt::Horizontal, ui->myTable); ui->myTable->setHorizontalHeader(myHeader);
And even though it does setHorizontalHeader and looks like it might replace your existing header, it appears to only affect the first header column.
It creates a new Header that draws a check box image on itself since its not possible to insert a real widget.
It then response to mouse press to make the image work as a real check box.
it checks with
if (logicalIndex == 0)
so I think it only paints this in first column.