QSqlTableModel - setFilter cannot be validated



  • 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?



  • 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()?



  • 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).



  • "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().



  • 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.



  • [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/.



  • 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))";
    #endif

    QSqlQuery query(db);
    if(!query.exec&#40;createStatement&#41;&#41; {
        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;
    

    }
    @



  • 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 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) :-)



  • that's the first thing i did



  • [quote author="harry" date="1302197599"]that's the first thing i did [/quote]

    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.