QSqlTableModel - setFilter cannot be validated
-
wrote on 5 Apr 2011, 15:42 last edited by
setFilter is a void function, so setting an invalid filter, you don't get an indication of something being wrong.
Even after you run a select(), there is no indication of any error (lastError() is NoError) [but at least no data is returned].Is that really so, or is there some flag or variable that one can inspect to see something went wrong?
-
wrote on 5 Apr 2011, 16:00 last edited by
It is not the case, that a filter that results in no data returned is automatically an invalid filter. Could you give us an example of a filter that causes this behavior, but does not result in an error for lastError()?
-
wrote on 5 Apr 2011, 16:15 last edited by
Sure, a filter can return no data.
But if the filter is wrong for example by using columns that don't exist in the the table, there is no indication as to whether the filter didn't return data or it was an invalid filter.
(eg. a table with two columns id and descr, if I specify as a filter id2>5, there is no error as far as I know). -
wrote on 5 Apr 2011, 21:24 last edited by
"QSqlTableModel::setFilter() ":http://doc.qt.nokia.com/4.7/qsqltablemodel.html#setFilter calls "QSqlTableModel::select() ":http://doc.qt.nokia.com/4.7/qsqltablemodel.html#select. If there is an error, you should get it via "QSqlQueryModel::lastError() ":http://doc.qt.nokia.com/4.7/qsqlquerymodel.html#lastError - do you check that after your call to setFilter().
-
wrote on 5 Apr 2011, 21:31 last edited by
Yes I do, that's exactly what puzzles me.
There is no error, where I think there should be.
I've tried with both a QPSQL and QODBC database. -
wrote on 6 Apr 2011, 05:29 last edited by
[quote author="hsfougaris" date="1302020154"]Sure, a filter can return no data.
But if the filter is wrong for example by using columns that don't exist in the the table, there is no indication as to whether the filter didn't return data or it was an invalid filter.
(eg. a table with two columns id and descr, if I specify as a filter id2>5, there is no error as far as I know).
[/quote]If that is the case, you have found a bug, I think. Please report it to "Jira":http://bugreports.qt.nokia.com/.
-
wrote on 7 Apr 2011, 16:49 last edited by
The bug report for this is "QTBUG-18578":http://bugreports.qt.nokia.com/browse/QTBUG-18578
The table model works correctly. All errors are catched and accessible.
See the following sample code, tested with SQLite and PostgreSQL. I doubt it behaves different on other drivers.
@
#include <QDebug>
#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlTableModel>int main(int argc, char *argv[])
{
QApplication a(argc, argv);#if 0
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/Users/volker/src/QtDevNet/WindowTest/devnet.sqlite");
if(!db.open()) {
qDebug() << "db: " << db.lastError();
return 1;
}
QString createStatement = "CREATE TABLE if not exists devnet (id number, name varchar(100))";
#endif#if 1
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("server");
db.setDatabaseName("dbname");
db.setUserName("user");
db.setPassword("pass");
if(!db.open()) {
qDebug() << "db: " << db.lastError();
return 1;
}
QString createStatement = "CREATE TEMPORARY TABLE devnet (id int, name varchar(100))";
#endifQSqlQuery query(db); if(!query.exec(createStatement)) { qDebug() << "query: " << query.lastError(); qDebug() << "db : " << db.lastError(); return 1; } query.clear(); QSqlTableModel tm(0, db); tm.setTable("devnet"); if(!tm.select()) { qDebug() << "model: " << tm.lastError(); qDebug() << "db : " << db.lastError(); return 1; } else { qDebug() << "model: " << tm.lastError(); qDebug() << "db : " << db.lastError(); } tm.setFilter("id2 > 0"); qDebug() << "after setFilter:"; qDebug() << "model: " << tm.lastError(); qDebug() << "db : " << db.lastError(); bool ok = tm.select(); qDebug() << "after select: ok = " << ok; qDebug() << "model: " << tm.lastError(); qDebug() << "db : " << db.lastError(); return 0;
}
@ -
wrote on 7 Apr 2011, 17:22 last edited by
You're right... it works even in my code now.
Meanwhile I have replaced my ODBC driver due to other issues (support for greek), so it might have been an issue there. -
wrote on 7 Apr 2011, 17:32 last edited by
[quote author="harry" date="1302196949"]You're right... it works even in my code now.
Meanwhile I have replaced my ODBC driver due to other issues (support for greek), so it might have been an issue there.[/quote]
Then please augment your bugreport with this information as well. No need to waste the developers time trying to triage or recreate your problem if it doesn't exist (anymore) :-)
-
wrote on 7 Apr 2011, 17:33 last edited by
that's the first thing i did
-
wrote on 7 Apr 2011, 17:41 last edited by
[quote author="harry" date="1302197599"]that's the first thing i did [/quote]
Thanks!
1/11