2014-03-31 44 views

回答

2

您可以對QHeaderView進行子類化,併爲您想要跨越的每個列/行組創建一個部分,並連接信號和插槽以使它們對不同的列/行做出反應。

下面的例子是用於跨越水平標頭:

#include <QtGui> 

class MyHeaderModel : public QAbstractItemModel 
{ 
public: 
    MyHeaderModel(QObject *parent = 0) : QAbstractItemModel(parent) {} 
    int columnCount(const QModelIndex &parent = QModelIndex()) const { return 2; } 
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const { return QVariant(); } 
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const { return QModelIndex(); } 
    QModelIndex parent(const QModelIndex &index) const { return QModelIndex(); } 
    int rowCount(const QModelIndex &parent = QModelIndex()) const { return 0; } 
}; 

class MyHeader : public QHeaderView 
{ 
    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: 
    bool eventFilter(QObject *o, QEvent *e) 
    { 
     if (o == mainHeader) { 
      if (e->type() == QEvent::Resize) { 
       setOffset(mainHeader->offset()); 
       setGeometry(0, 0, mainHeader->width(), mainHeader->height()); 
      } 
      return false; 
     } 
     return QHeaderView::eventFilter(o, e); 
    } 
private: 
    int getSectionSizes(int first, int second) 
    { 
     int size = 0; 
     for (int a=first;a<=second;++a) 
      size += mainHeader->sectionSize(a); 
     return size; 
    } 
    QHeaderView *mainHeader; 

}; 

#include "main.moc" 
int main(int argc, char **argv) 
{ 
    QApplication a(argc, argv); 
    QWidget w; 
    QVBoxLayout *vbox = new QVBoxLayout; 
    QTableWidget *tw = new QTableWidget(5, 5); 
    MyHeader *h = new MyHeader(tw->horizontalHeader()); 
    vbox->addWidget(tw); 
    w.setLayout(vbox); 
    w.show(); 
    return a.exec(); 
} 
+0

我已經跨過上面的片段來了。當我做這個項目時,出現了一個編譯錯誤。在上面的代碼片段中,包含main.moc,它不存在。我是Qt的新手,不知道moc文件是在編譯期間生成的,還是隻是另一個像頭文件(它必須由程序員編寫)的文件。請相應地指導我。 – user3480065

+0

我可以編譯代碼。對Makefile做了一些小的改動,並且做了詭計! – user3480065