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