QTableView sorting problem
Hello!
I'm new to Qt and I'm using Qt eclipse integration with version 4.3.4 in Windows XP.
I've created a QTableView in the Eclipse's designer. The tableview is filled with a QSqlTableModel from a SQLite database and I set the sortingEnabled property to true.
When running the program and click in a column header all the table goes empty (the data and the headers)...
This is the code I have:
Code:
db.setDatabaseName("stock.sqlite");
if (!db.
open()) QMessageBox::critical(0,
QObject::tr("Database Error"), db.
lastError().
text());
model->setTable("productes");
model->select();
ui.tableView->setSortingEnabled(true);
ui.tableView->setModel(model);
What am I doing wrong? I tried a lot of things ( changing the propertys in the designer or after the setModel and I got the same result).
Thanks in advance.
Re: QTableView sorting problem
Could you provide a minimal compilable example reproducing the problem?
Re: QTableView sorting problem
My experience with this comination is : allways use
Code:
while (myQueryModel->canFetchMore()){
myQueryModel->fetchMore();
}
If this solves your Problem: I dont know ;)
btw: sqlite + QSqlTabeleModel is a pain in the ass :rolleyes:
I use QSqlQueryModel. But then sorting does not work :eek:
1 Attachment(s)
Re: QTableView sorting problem
Thank you Janus, but this do not sove my problem... :(
A compilable code:
Code:
#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QSqlTableModel>
#include <QTableView>
#include <QtGui>
#include <QtSql>
int main(int argc, char *argv[])
{
//QWidget *w = new QWidget;
db.setDatabaseName("stock.sqlite");
if (!db.
open()) QMessageBox::critical(0,
QObject::tr("Database Error"), db.
lastError().
text());
model->setTable("productes");
model->select();
tableView->setSortingEnabled(true);
tableView->setModel(model);
db.close();
tableView->show();
//w->show();
return app.exec();
}
this can be tried with the database attached (it is very small), i put the .txt extension to upload it, but it is in sqlite3 format.
I was trying (for one week) to use a mysql database, but I can't compile the driver, I know that are lots of threads and lots of tutorials, but I always get a compile error, is there a really working way to compile/load the mysql driver?
Re: QTableView sorting problem
I had a similar problem that went me crazy, but it was with QTableWidget, maybe it could give you some clou.
My problem was solved by disabling the sorting before table filling and reenabling it after fill was complete, so something like:
Code:
void myClass::populateTable()
{
table->setSortingEnabled(false);
// table fill operations.... [...]
table->setSortingEnabled(true);
}
I hope this can somehow help.
Re: QTableView sorting problem
i modified it a little bit. But at least it works like this ...
Code:
tablesort
::tablesort(QWidget *parent
){
ui.setupUi(this);
connect(tableView->horizontalHeader(), SIGNAL(sectionClicked(int)),this, SLOT(err()));
db.setDatabaseName("test");
if (!db.
open()) QMessageBox::critical(0,
QObject::tr("Database Error"), db.
lastError().
text());
model->setTable("tbl1");
model->select();
tableView->setSortingEnabled(true);
tableView->setModel(model);
// db.close();
}
void tablesort::err()
{
}
Re: QTableView sorting problem
Thank you janus! it works now.
The error was that i was showing te widget from a Dialog, and then i get a "Driver not loaded" error when clicking in the header.
Re: QTableView sorting problem
It only works if only the widget is shown, if I do an exec of a QDialog before or after the widget is shown, when I click in the header driver not loaded error happens.
I don't know if it is a problem of the database, in the installation of QT or in my application.
I prefer to use another database, better mysql, but I'm not able to compile the drivers...
Re: QTableView sorting problem
Wrap the model into QSortFilterProxyModel and forget about all the problems.
Re: QTableView sorting problem
Quote:
Originally Posted by
noktus
It only works if only the widget is shown, if I do an exec of a QDialog before or after the widget is shown, when I click in the header driver not loaded error happens.
Could you post the complete code?
Re: QTableView sorting problem
Finally the problem was solved using the QSortFilterProxyModel class that wysota recommends.
Thanks for your concern janus :D
The code that I was using to try was:
Code:
myDialog dialog;
dialog.exec();
formUser frm1;
frm1.show();
And the formUser only has the table.
Re: QTableView sorting problem
@noktus
Dont mind. it is also concern for my code :)
@wysota
QSortFilterProxyModel helped me as well. Now sorting even works with a queryModel :D ( I can not use QSqlTableModel because it locks the sqlite database). AND sorting with the QSqlTableModel did change the rowhight (table->resizeRowsToContents() did not work). So thx. This solves two problems I had.