PDA

View Full Version : QTableView sorting problem



noktus
21st April 2008, 13:26
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:


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("stock.sqlite");
if (!db.open()) QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());

model = new QSqlTableModel(this);
model->setTable("productes");
model->select();
ui.tableView->setSortingEnabled(true);
ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui.tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
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.

wysota
21st April 2008, 13:36
Could you provide a minimal compilable example reproducing the problem?

janus
21st April 2008, 15:09
My experience with this comination is : allways use

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:

noktus
22nd April 2008, 11:07
Thank you Janus, but this do not sove my problem... :(

A compilable code:

#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QSqlTableModel>
#include <QTableView>
#include <QtGui>
#include <QtSql>

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

//QWidget *w = new QWidget;

QTableView * tableView = new QTableView();
QSqlTableModel *model;

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("stock.sqlite");
if (!db.open()) QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());

model = new QSqlTableModel(tableView);
model->setTable("productes");
model->select();
tableView->setSortingEnabled(true);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
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?

Raccoon29
22nd April 2008, 12:36
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:


void myClass::populateTable()
{
table->setSortingEnabled(false);
// table fill operations.... [...]
table->setSortingEnabled(true);
}

I hope this can somehow help.

janus
22nd April 2008, 12:39
i modified it a little bit. But at least it works like this ...


tablesort::tablesort(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);

QTableView * tableView = new QTableView(this);

connect(tableView->horizontalHeader(), SIGNAL(sectionClicked(int)),this, SLOT(err()));


db = QSqlDatabase::addDatabase("QSQLITE");

db.setDatabaseName("test");

if (!db.open()) QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());

model = new QSqlTableModel(this);
model->setTable("tbl1");
model->select();

tableView->setSortingEnabled(true);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
tableView->setModel(model);
// db.close();
}

void tablesort::err()
{

QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());

}

noktus
22nd April 2008, 13:35
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.

noktus
22nd April 2008, 15:22
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...

wysota
22nd April 2008, 21:27
Wrap the model into QSortFilterProxyModel and forget about all the problems.

janus
23rd April 2008, 09:44
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?

noktus
23rd April 2008, 10:19
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:

myDialog dialog;
dialog.exec();

formUser frm1;
frm1.show();

And the formUser only has the table.

janus
23rd April 2008, 11:20
@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.