PDA

View Full Version : qtableview:one table hide Column,and other tables not



chenmo20074639
8th May 2014, 05:22
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.

ChrisW67
8th May 2014, 06:32
Visibility of columns in a view is independent of other views using the model. Visibility is not passed through the model. For example:


#include <QApplication>
#include <QStandardItemModel>
#include <QTableView>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QStandardItemModel model(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model.setItem(row, column, item);
}
}

QTableView view1;
view1.setModel(&model);
view1.setColumnHidden(2, true); // only hidden in this view
view1.resize(800, 300);
view1.move(0, 0);
view1.show();

QTableView view2;
view2.setModel(&model);
view2.resize(800, 300);
view2.move(0, 320);
view2.show();

return app.exec();
}

10343

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

chenmo20074639
8th May 2014, 07:19
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();

}

ChrisW67
8th May 2014, 07:36
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.

chenmo20074639
8th May 2014, 10:11
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.

ChrisW67
8th May 2014, 22:40
I mean that when you call setModel() the view makes all columns visible. You need to hide columns after you set the model.

chenmo20074639
9th May 2014, 04:54
Thank you very much. I think I could solve this problem with your method now.