How can I span the headers in my QTableView ?

Qt does not provide an API for spanning headers, but you can implement this yourself. You can subclass QHeaderView [doc.qt.nokia.com] and create one section for each of the groups of columns/rows you would like to span and connect signals and slots to have them react to the different columns/rows.

The following example demonstrates how this can be done for spanning thecolumns in the horizontal header.

  1. #include <QtGui>
  2.  
  3. class MyHeaderModel : public QAbstractItemModel
  4. {
  5. public:
  6.     MyHeaderModel(QObject *parent = 0) : QAbstractItemModel(parent) {}
  7.     int columnCount(const QModelIndex &parent = QModelIndex()) const { return 2; }
  8.     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const { return QVariant(); }
  9.     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const { return QModelIndex(); }
  10.     QModelIndex parent(const QModelIndex &index) const { return QModelIndex(); }
  11.     int rowCount(const QModelIndex &parent = QModelIndex()) const { return 0; }
  12. };
  13.  
  14. class MyHeader : public QHeaderView
  15. {
  16.     Q_OBJECT
  17. public:
  18.     MyHeader(QHeaderView *header, QWidget *parent = 0) : QHeaderView(Qt::Horizontal, header), mainHeader(header)
  19.     {
  20.         setModel(new MyHeaderModel(this));
  21.         // This example uses hardcoded groups, you can extend
  22.         // this yourself to save the groups
  23.         // Group 1 is 0-2 and Group 2 is 3-4
  24.         resizeSection(0, getSectionSizes(0, 2));
  25.         resizeSection(1, getSectionSizes(3, 4));
  26.         connect(this, SIGNAL(sectionResized(int,int,int)), this, SLOT(updateSizes()));
  27.         connect(((QTableWidget *)(mainHeader->parentWidget()))->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(updateOffset()));
  28.         setGeometry(0, 0, header->width(), header->height());
  29.         updateOffset();
  30.         mainHeader->installEventFilter(this);
  31.     }
  32. public slots:
  33.     void updateSizes()
  34.     {
  35.         setOffset(mainHeader->offset());
  36.         mainHeader->resizeSection(2, mainHeader->sectionSize(2) + (sectionSize(0) - getSectionSizes(0, 2)));
  37.         mainHeader->resizeSection(4, mainHeader->sectionSize(4) + (sectionSize(1) - getSectionSizes(3, 4)));
  38.     }
  39.     void updateOffset()
  40.     {
  41.         setOffset(mainHeader->offset());
  42.     }
  43. protected:
  44.     bool eventFilter(QObject *o, QEvent *e)
  45.     {
  46.         if (o == mainHeader) {
  47.             if (e->type() == QEvent::Resize) {
  48.                 setOffset(mainHeader->offset());
  49.                 setGeometry(0, 0, mainHeader->width(), mainHeader->height());
  50.             }
  51.             return false;
  52.         }
  53.         return QHeaderView::eventFilter(o, e);
  54.     }
  55. private:
  56.     int getSectionSizes(int first, int second)
  57.     {
  58.         int size = 0;
  59.         for (int a=first;a<=second;++a)
  60.             size += mainHeader->sectionSize(a);
  61.         return size;
  62.     }
  63.     QHeaderView *mainHeader;
  64.  
  65. };
  66.  
  67. #include "main.moc"
  68. int main(int argc, char **argv)
  69. {
  70.     QApplication a(argc, argv);
  71.     QWidget w;
  72.     QVBoxLayout *vbox = new QVBoxLayout;
  73.     QTableWidget *tw = new QTableWidget(5, 5);
  74.     MyHeader *h = new MyHeader(tw->horizontalHeader());
  75.     vbox->addWidget(tw);
  76.     w.setLayout(vbox);
  77.     w.show();
  78.     return a.exec();
  79. }
  80.  

2 comments

July 8, 2010

Picture of nicola1 nicola1

Lab Rat

change “</notextile>” in “==”

July 9, 2010

Picture of nicola1 nicola1

Lab Rat

If you want add column name, add QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
in the class MyHeaderModel.

example:

  1. QVariant headerData(int section, Qt::Orientation orientation,  int role = Qt::DisplayRole) const
  2.     &#123;
  3.         if (role == Qt::DisplayRole) &#123;
  4.             if (section == 0)
  5.                 return QVariant("Row 1");
  6.             else if (section == 1)
  7.                 return QVariant("Row 2");
  8.         &#125;
  9.         return QVariant();
  10.     &#125;

Write a comment

Sorry, you must be logged in to post a comment.