View Full Version : How to add custom header to QTableWidget

20th May 2014, 21:46
How can I add a custom header to my QTableWidget?

I have derived a class MyHeaderItem from QWidget and QTableWidgetItem and set an instance of it into the table. I have copied the code below.

This does not work. Column 0 in the table shows an empty header. What am I missing?

I need a custom header because I want to place several strings and icons in the header.
Is there some other technique to add a custom header to QTableWidget?

class MyHeaderItem : public QWidget, public QTableWidgetItem
MyHeaderItem( QWidget* parent ) : QWidget( parent ), QTableWidgetItem( 0 )
mWidget.setupUi( this );

Ui_simple_widget mWidget;

MyHeaderItem* headerItem = new MyHeaderItem( myTable );
myTable->setHorizontalHeaderItem( 0, headerItem );

21st May 2014, 04:20
If your MyHeaderItem is derived from QWidget, then you need the Q_OBJECT macro in the class definition. Besides that, I have no idea if it is even possible to use a QWidget as a header item. You don't show that you have implemented the clone() method, and I would think that would be an important one.

21st May 2014, 08:48
See below link, in which example is given for Hierarchical headers for QTableView, see if it can help you to get some idea for QTableWidget.