PDA

View Full Version : How to add custom header to QTableWidget



alex_25
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
{
public:
MyHeaderItem( QWidget* parent ) : QWidget( parent ), QTableWidgetItem( 0 )
{
mWidget.setupUi( this );
}

private:
Ui_simple_widget mWidget;
};

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

d_stranz
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.

Rajesh.Rathod
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.

http://qt-apps.org/content/show.php/HierarchicalHeaderView?content=103154