- All (478)
- jom (0)
- Qt Linguist (7)
- Qt Eclipse Integration (9)
- Qt Designer (7)
- Qt Creator (4)
- Qt build system: qmake (31)
- Qt build system: configure (3)
- Qt Assistant (5)
- Printing (4)
- Porting from Qt 3 to Qt 4 (1)
- Plugins (7)
- Qt Visual Studio AddIn (2)
- Qt/MFC Migration (2)
- QtScript (3)
- MDI (2)
- XML (1)
- Widgets (22)
- WebKit (5)
- Tools and Containers (2)
- Threads (2)
- Text Handling (10)
- SQL (6)
- QtTest (1)
- QtService (1)
- Platform: Windows (49)
- Platform: Unix (16)
- Platform: Mac OS X (18)
- Image Formats (2)
- I/O (2)
- Graphicsview (8)
- Font handling (9)
- Event System (18)
- Drag and Drop (4)
- Dialogs (6)
- Desktop integration (3)
- ActiveQt (3)
- Itemviews (60)
- Layout (4)
- Qt Quick (10)
- Qt SDK (1)
- Licensing (2)
- Platform: Embedded Linux (38)
- Painting (32)
- OpenGL (4)
- Object Model (6)
- Network (5)
- Multimedia (3)
- Miscellanous (23)
- Main Window (19)
- Look and Feel (23)
- Development (0)
- Getting Involved (0)
- Routines (0)
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.
- #include <QtGui>
- {
- public:
- QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const { return QModelIndex(); }
- };
- {
- Q_OBJECT
- public:
- MyHeader(QHeaderView *header, QWidget *parent = 0) : QHeaderView(Qt::Horizontal, header), mainHeader(header)
- {
- setModel(new MyHeaderModel(this));
- // This example uses hardcoded groups, you can extend
- // this yourself to save the groups
- // Group 1 is 0-2 and Group 2 is 3-4
- resizeSection(0, getSectionSizes(0, 2));
- resizeSection(1, getSectionSizes(3, 4));
- connect(this, SIGNAL(sectionResized(int,int,int)), this, SLOT(updateSizes()));
- connect(((QTableWidget *)(mainHeader->parentWidget()))->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(updateOffset()));
- setGeometry(0, 0, header->width(), header->height());
- updateOffset();
- mainHeader->installEventFilter(this);
- }
- public slots:
- void updateSizes()
- {
- setOffset(mainHeader->offset());
- mainHeader->resizeSection(2, mainHeader->sectionSize(2) + (sectionSize(0) - getSectionSizes(0, 2)));
- mainHeader->resizeSection(4, mainHeader->sectionSize(4) + (sectionSize(1) - getSectionSizes(3, 4)));
- }
- void updateOffset()
- {
- setOffset(mainHeader->offset());
- }
- protected:
- {
- if (o == mainHeader) {
- setOffset(mainHeader->offset());
- setGeometry(0, 0, mainHeader->width(), mainHeader->height());
- }
- return false;
- }
- }
- private:
- int getSectionSizes(int first, int second)
- {
- int size = 0;
- for (int a=first;a<=second;++a)
- size += mainHeader->sectionSize(a);
- return size;
- }
- };
- #include "main.moc"
- int main(int argc, char **argv)
- {
- QWidget w;
- MyHeader *h = new MyHeader(tw->horizontalHeader());
- vbox->addWidget(tw);
- w.setLayout(vbox);
- w.show();
- return a.exec();
- }

2 comments
July 8, 2010
Lab Rat
change “</notextile>” in “==”
July 9, 2010
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: