Results 1 to 7 of 7

Thread: qtableview:one table hide Column,and other tables not

  1. #1
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default qtableview:one table hide Column,and other tables not

    Hi all,
    I'm developing a software on QT5.1.0. It mainly deals with database .
    And I use Qtableview and QSqlRelationalTableModel to all the tables.
    And use hideColumn or setColumnHidden to hide a column in one table, I don't want the other tables hide the column, but it do it.
    Could somebody give me some ideas???
    My english is so poor that the post maybe has some syntax errors.
    Last edited by chenmo20074639; 8th May 2014 at 04:36.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qtableview:one table hide Column,and other tables not

    Visibility of columns in a view is independent of other views using the model. Visibility is not passed through the model. For example:
    Qt Code:
    1. #include <QApplication>
    2. #include <QStandardItemModel>
    3. #include <QTableView>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QStandardItemModel model(4, 4);
    10. for (int row = 0; row < 4; ++row) {
    11. for (int column = 0; column < 4; ++column) {
    12. QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    13. model.setItem(row, column, item);
    14. }
    15. }
    16.  
    17. QTableView view1;
    18. view1.setModel(&model);
    19. view1.setColumnHidden(2, true); // only hidden in this view
    20. view1.resize(800, 300);
    21. view1.move(0, 0);
    22. view1.show();
    23.  
    24. QTableView view2;
    25. view2.setModel(&model);
    26. view2.resize(800, 300);
    27. view2.move(0, 320);
    28. view2.show();
    29.  
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 
    tables.jpg

    Where are your calls to setColumnHidden()? In the constructor for a class?

  3. The following user says thank you to ChrisW67 for this useful post:

    chenmo20074639 (8th May 2014)

  4. #3
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qtableview:one table hide Column,and other tables not

    I use the same Qtableview and QSqlRelationalTableModel to show all the tables. And I just call the setColumnHidden() in one table which want to hide the column. All other tables don't call this function.
    eg:

    {
    PA_table_model->setTable(qobject_cast<QPushButton*>(sender())->objectName());

    PA_table_model->select();

    QTableView *pa_table_view = centralWidget_PA->findChild<QTableView *>("tableView_pa");

    pa_table_view->hideColumn(0);
    // pa_table_view->setColumnHidden(0,true);


    pa_table_view->setModel(PA_table_model);


    pa_table_view->show();

    }

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qtableview:one table hide Column,and other tables not

    You should set the model on the view before you try to hide columns or rows. Setting the model on the view resets the visibility of columns or rows (the column/row count might have changed etc).

    BTW: Your code is fragile, the qobject_cast<T>() and findChild<T>() calls could return 0 and crash the program if you modify the UI.

  6. #5
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qtableview:one table hide Column,and other tables not

    Thanks for your help. I'm not very understand the meaning of the " resets the visibility of columns or rows".
    Does it mean that I should call showColumn somewhere?
    And if you hadn't reminded me of the code matter, I would certainly have passed it. Thanks a lot.
    Last edited by chenmo20074639; 8th May 2014 at 10:07.

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qtableview:one table hide Column,and other tables not

    I mean that when you call setModel() the view makes all columns visible. You need to hide columns after you set the model.

  8. #7
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qtableview:one table hide Column,and other tables not

    Thank you very much. I think I could solve this problem with your method now.

Similar Threads

  1. Replies: 7
    Last Post: 14th August 2015, 20:49
  2. list tables column name (database)
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 26th September 2012, 11:18
  3. different column widgets in trees or tables
    By fulbay in forum Qt Programming
    Replies: 1
    Last Post: 6th October 2010, 15:28
  4. Joining tables into QTableView
    By SykeS in forum Qt Programming
    Replies: 10
    Last Post: 8th June 2010, 10:39
  5. Way to load all of data from 2 tables into one QTableView?
    By Kevin Hoang in forum Qt Programming
    Replies: 8
    Last Post: 3rd April 2010, 09:42

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.