View Full Version : Can I have a qwidget in the headerData of a Table?

14th April 2011, 15:41
I would like to have a checkbox in the (column) headers in a QTableView() that I use, in order to mark certain columns. The model that I use is a QSqlTableModel. However, I can't find a way to do that.
Maybe somebody knows the trick?


14th April 2011, 22:58
I've added a combo box to the header of a QTableWidget. I assume it works similar with a QTableView. Here's an outline of what I've done:
- Create a new class MyHeaderView derived from QHeaderView
- In the CTOR of MyHeaderView, I create a new QComboBox with the header view viewport as parent:
new QComboBox(viewport())
- In the CTOR, connect QHeaderView signal sectionResized(int,int,int) to a slot onSectionResized(int,int,int) of the new class
MyHeaderView. This slot is responsible for moving my combo box to the right place and resize it in case that the section
size changes.
- Implement onSectionResized(int section, int oldSize, int newSize) in class MyHeaderView. In my implementation, I use
QSize s = sectionSizeFromContents(section) to get width and height.
For moving the combo box, I use sectionViewportPosition(section) to determine the section start position.

Hope this will help you a little bit.