Results 1 to 5 of 5

Thread: Same QHeaderView for several tables

  1. #1
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Same QHeaderView for several tables

    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:

    Qt Code:
    1. #include <QtGui>
    2. #include "mytabwidget.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. MyTabWidget w;
    9.  
    10. w.setWindowTitle("TEST");
    11. w.setWindowState(Qt::WindowMaximized);
    12. w.show();
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    mytabwidget.h:

    Qt Code:
    1. #ifndef MYTABWIDGET_H
    2. #define MYTABWIDGET_H
    3.  
    4. #include <QtGui>
    5.  
    6. class MyTabWidget : public QTabWidget{
    7. Q_OBJECT
    8.  
    9. public:
    10. MyTabWidget(QWidget *parent = 0);
    11.  
    12. private:
    13. QHeaderView *table_header;
    14.  
    15. private slots:
    16. void change_header(int);
    17.  
    18. };
    19.  
    20. #endif // MYTABWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    mytabwidget.cpp:

    Qt Code:
    1. #include "mytabwidget.h"
    2.  
    3. MyTabWidget::MyTabWidget(QWidget *parent) : QTabWidget(parent){
    4. table_header = new QHeaderView(Qt::Horizontal);
    5. table_header->setMovable(true);
    6.  
    7. QStandardItemModel *model = new QStandardItemModel(20, 10);
    8. for(int i = 0; i < model->rowCount(); i++){
    9. for(int j = 0; j < model->columnCount(); j++)
    10. model->setData(model->index(i,j), i*j);
    11. }
    12.  
    13. QTableView *table1 = new QTableView();
    14. QTableView *table2 = new QTableView();
    15. QTableView *table3 = new QTableView();
    16.  
    17. table1->setModel(model);
    18. table2->setModel(model);
    19. table3->setModel(model);
    20.  
    21. table1->setHorizontalHeader(table_header);
    22. table2->setHorizontalHeader(table_header);
    23. table3->setHorizontalHeader(table_header);
    24.  
    25. this->addTab(table1, "table1");
    26. this->addTab(table2, "table2");
    27. this->addTab(table3, "table3");
    28.  
    29. change_header(0);
    30.  
    31. QObject::connect(this, SIGNAL(currentChanged(int)), SLOT(change_header(int)));
    32. }
    33.  
    34. void MyTabWidget::change_header(int index){
    35. QTableView *d = (QTableView*)currentWidget();
    36. table_header->setParent(this->currentWidget());
    37. table_header->show();
    38. }
    To copy to clipboard, switch view to plain text mode 

    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:

    tKziI0mm70Y.jpg

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

  2. The following user says thank you to qks1 for this useful post:


  3. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Same QHeaderView for several tables

    apart from adding a table_header->update(); after/before the table_header->show(), I don't know!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #3
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Same QHeaderView for several tables

    Any ideas?
    update() didn't change anything

  5. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Same QHeaderView for several tables

    can't you make three headers and keep them in-synch?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #5
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Same QHeaderView for several tables

    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.

Similar Threads

  1. Replies: 4
    Last Post: 13th July 2010, 05:17
  2. Replies: 1
    Last Post: 16th May 2010, 19:25
  3. tables
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 15:17
  4. nested tables
    By Jeroen van der Waal in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2007, 17:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.