View Full Version : QTableView sorting
gabriels
1st February 2006, 19:49
Hi,
I use Qt v4.0.1 (OpenSource) on the Win XP.
My app query some rows from the mysql server and show it into the QTableView.
What I want is to sort the QTableView when the header column is clicked.
I tried to use this slot but it didn't work :
void QTableView::sortByColumn ( int column ) [slot]
( Sorts the model by the values in the given column.)
Can anybody help me or give me some suggestions, please?
Thanks,
Gabriel S
These are parts of my code :
.............
QTableView *tableView = new QTableView(WindMaincall);
QSqlQueryModel *model = new QSqlQueryModel(tableView);
connect(tableView->horizontalHeader(),SIGNAL(sectionClicked(int)), tableView, SLOT(sortByColumn(int)));
...................
model->setQuery( "SELECT field1,field2 FROM table1" );
tableView->setModel(model);
KjellKod
1st February 2006, 21:39
What do you mean that it didn't work? Can you provide some more info?
--- I haven't tried that function on QTableView, but I've tried it's 'cousin' function on QTableWidget
void QTableWidget::sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder )
Sorts all the rows in the table widget based on column and order.
And that worked w/out any problems (similar to your code)
Everall
2nd February 2006, 10:15
have you used
setClickable(true) on your QHeaderView?
cheers
gabriels
2nd February 2006, 18:59
Thanks to KjellKod and Everal,
The problem is that when the header of the QTableView is clicked, the slot sortByColumn which is
the receiver from the signal "sectionClicked" it doesn't do anything.
I can't use the QTableWidget instead of QTableView, because I must use the QSqlQueryModel as a "data source " for the QTableView.
After I have take a loock on the Qt sorces I think that I found the problems:
On the qtableview.cpp I found this :
void QTableView::sortByColumn(int column)
{
Q_D(QTableView);
if (!d->model)
return;
bool ascending = (horizontalHeader()->sortIndicatorSection() == column
&& horizontalHeader()->sortIndicatorOrder() == Qt::DescendingOrder);
Qt::SortOrder order = ascending ? Qt::AscendingOrder : Qt::DescendingOrder;
horizontalHeader()->setSortIndicator(column, order);
d->model->sort(column, order);
}
So, this metod call the model->sort(column,order) of the QSqlQUeryModel (in my case).
Loocking on the documentation from the QSqlQueryModel::sort (...), I found this :
*!
Sorts the model by \a column in the given \a order.
The base class implementation does nothing.
*/
void QAbstractItemModel::sort(int column, Qt::SortOrder order)
{
Q_UNUSED(column);
Q_UNUSED(order);
// do nothing
}
It seems than I must subclassing the sort method...but I don't beleive than to sort a QTableView can be so complicated ..
Thanks ,
Gabriel Strimtu
Everall
3rd February 2006, 13:06
model->setQuery( "SELECT field1,field2 FROM table1" );
You are using not more than 1 table so QSqlTableModel fits your needs?
instead of
QSqlQueryModel *model = new QSqlQueryModel(tableView);
you could use
QSqlTableModel *model = new QSqlTableModel(tableView);
which has
void QSqlTableModel::sort ( int column, Qt::SortOrder order ) [virtual]
implemented
Cheers
gabriels
3rd February 2006, 17:46
model->setQuery( "SELECT field1,field2 FROM table1" );
Thanks Everall,
No, I 'must to use 3 tables on my sql query and for this reason I can't use the QSQlTableModel.Sorry for my wrong SELECT, was just an bad example.
I solve the problem using now the QTableWidget insted of QTableView but I should write code to populate it.
The good news is that in order to sort the QTableWidget I just call :
void setSortingEnabled ( bool enable )
and it is sorted. No more code is necessary :)
I'm sorry for the unimplemented sort metod of the QSqlQueryModel..I think that is a gap .
Regards,
Gabriel Strimtu
swinnenb
1st June 2006, 22:07
Hi
Using Qt 4.1.3 (release, linux)
The actual problem is in QTableView.
It is not possible to enable sorting in QTableView as the actual connect to sortByColumn is made in QTableWidget and not in QtableView.
If you compare this with QTreeView, you'll notice that these connects are moved to QTreeView.
This should be done by Trolltech guys as well for QTableView.
Just compare code difference in qt source where they connect sortByColumn and you'll understand.
BTW: When using sort function in your widget you'll notice that the sort works fine in QTableView. Only the connections when clicking the header are made at the wrong place (In QTableWidget instead of in QTableView).
I hope this explains the original problem in this topic.
And I hope they come with a fix. By the way I did not check snapshot code to see if it is already fixed.
Greetzzzzzz
Bart
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.