PDA

View Full Version : Same QHeaderView for several tables



qks1
19th November 2012, 15:03
Hi,

sorry for mistakes, my English is so bad, I know.
I want to create a tabwidget with the same type of tables of each tab. i.e. each table may content the different data, but the columns, their order and their sizes must be the same, and when user changing columns order or width on one of the tabs, other tabs must to do it automatically. The must obvious way to do it I thought is to create one QHeaderView and set it for each table. Yes, I know that the widget can have only one parent, so I'm changing the parent of the header when change tab. Here's the very simple example:

main.cpp:


#include <QtGui>
#include "mytabwidget.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

MyTabWidget w;

w.setWindowTitle("TEST");
w.setWindowState(Qt::WindowMaximized);
w.show();

return a.exec();
}

mytabwidget.h:


#ifndef MYTABWIDGET_H
#define MYTABWIDGET_H

#include <QtGui>

class MyTabWidget : public QTabWidget{
Q_OBJECT

public:
MyTabWidget(QWidget *parent = 0);

private:
QHeaderView *table_header;

private slots:
void change_header(int);

};

#endif // MYTABWIDGET_H

mytabwidget.cpp:


#include "mytabwidget.h"

MyTabWidget::MyTabWidget(QWidget *parent) : QTabWidget(parent){
table_header = new QHeaderView(Qt::Horizontal);
table_header->setMovable(true);

QStandardItemModel *model = new QStandardItemModel(20, 10);
for(int i = 0; i < model->rowCount(); i++){
for(int j = 0; j < model->columnCount(); j++)
model->setData(model->index(i,j), i*j);
}

QTableView *table1 = new QTableView();
QTableView *table2 = new QTableView();
QTableView *table3 = new QTableView();

table1->setModel(model);
table2->setModel(model);
table3->setModel(model);

table1->setHorizontalHeader(table_header);
table2->setHorizontalHeader(table_header);
table3->setHorizontalHeader(table_header);

this->addTab(table1, "table1");
this->addTab(table2, "table2");
this->addTab(table3, "table3");

change_header(0);

QObject::connect(this, SIGNAL(currentChanged(int)), SLOT(change_header(int)));
}

void MyTabWidget::change_header(int index){
QTableView *d = (QTableView*)currentWidget();
table_header->setParent(this->currentWidget());
table_header->show();
}


Ok, there's almost the necessary functionality: columns are changing on each tabs when changing one of them. BUT sometimes changing the width I get following:

8437

The right part of the header and the left part of the table are not displayed! At the same time, after I open the last tab (table 3), the bug does not appear anymore, each tab works perfect! What a strange behavior? Is there any way to fix it? Or should I look another solution for my problem...

amleto
19th November 2012, 23:52
apart from adding a table_header->update(); after/before the table_header->show(), I don't know!

qks1
21st November 2012, 10:05
Any ideas?
update() didn't change anything :confused:

amleto
21st November 2012, 11:38
can't you make three headers and keep them in-synch?

qks1
21st November 2012, 12:28
Yes, it's now made so (signal emitted by header and cycle in parent), but I'm sure it's not the most simple and quick realization. In addition, the phenomenon described in the first post made ​​me great curiosity. So solution of the problem is not crucial, but I would be very pleased.