PDA

View Full Version : QTableView sorting



Bojan
9th March 2006, 03:59
What's the most proper way of sorting a QTableView? QSortFilterProxyModel doesn't seem to work on QTableViews. At least I can't get it to work. For example if I take the sorting treeview example, and just change it so there is a QTableView, it doesn't work:


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

QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel model(file.readAll());
file.close();

QSortFilterProxyModel sortingModel;
sortingModel.setSourceModel(&model);

QTableView sortedView;
sortedView.setModel(&sortingModel);
sortedView.setWindowTitle("Sorted Data");
sortedView.setSelectionBehavior(QAbstractItemView: :SelectRows);
sortedView.horizontalHeader()->setSortIndicator(0, Qt::AscendingOrder);
sortedView.horizontalHeader()->setSortIndicatorShown(true);
sortedView.horizontalHeader()->setClickable(true);
sortedView.verticalHeader()->setVisible(false);
sortedView.show();

return app.exec();
}

I would like to be able to sort the rows based on the column data in the clicked column. Qt 4.1. I don't need sample source code, just description of what to do, what things to connect, etc... Any advice is much appreciated.

Bojan

wysota
28th September 2006, 08:49
You should connect the sectionClicked signal from the header to sortByColumn slot in the view.

jpn
28th September 2006, 09:11
There is a method called QTableWidget::setSortingEnabled in Qt 4.1. This has been shifted to QTableView in Qt 4.2. Check it out to see what has to be done in order to enable sorting and disable selecting of rows/columns.



// ...
disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),
this, SLOT(selectColumn(int)));
connect(horizontalHeader(), SIGNAL(sectionClicked(int)),
this, SLOT(sortByColumn(int)));
/ ...