I am trying to extend the QTableView class to create a table that sorts by column when the column header is clicked. So far the only way I can see to do this is by connecting the QTableView's horizontalHeader's sectionClicked() signal to the QTableView's sortByColumn() slot, but nothing seems to happen when I click the column header other than the sortIndicator changing. How can I get the column to sort, how can I specify how the column should be sorted (ascending vs. descending)?
Here is the constructor for my implementation of QTableView. This is where I am connecting the signal to the slot. I am new to QT so it is quite possible I am doing this completely wrong. Any advice is appreciated.
{
this->setDragEnabled(false);
this->setAlternatingRowColors(true);
this->setAcceptDrops(false);
rowHeader->hide();
columnHeader->setClickable(true);
columnHeader->setSortIndicatorShown(true);
columnHeader->setSortIndicator(0, Qt::AscendingOrder);
QObject::connect(columnHeader,
SIGNAL(sectionClicked
(int)),
this,
SLOT(sortByColumn
(int)));
}
MyTableView::MyTableView(QWidget *parent) : QTableView(parent)
{
this->setDragEnabled(false);
this->setAlternatingRowColors(true);
this->setAcceptDrops(false);
QHeaderView* rowHeader = this->verticalHeader();
rowHeader->hide();
QHeaderView* columnHeader = this->horizontalHeader();
columnHeader->setClickable(true);
columnHeader->setSortIndicatorShown(true);
columnHeader->setSortIndicator(0, Qt::AscendingOrder);
QObject::connect(columnHeader, SIGNAL(sectionClicked(int)), this, SLOT(sortByColumn(int)));
}
To copy to clipboard, switch view to plain text mode
Thanks.
Bookmarks