Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlTableModel - setFilter cannot be validated
QtWS25 Last Chance

QSqlTableModel - setFilter cannot be validated

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 7.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hsfougaris
    wrote on last edited by
    #1

    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?

    If you can't say what you mean, you'll never be able to mean what you say.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hsfougaris
        wrote on last edited by
        #3

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

        If you can't say what you mean, you'll never be able to mean what you say.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

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

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hsfougaris
            wrote on last edited by
            #5

            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.

            If you can't say what you mean, you'll never be able to mean what you say.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                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;
                

                }
                @

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  hsfougaris
                  wrote on last edited by
                  #8

                  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.

                  If you can't say what you mean, you'll never be able to mean what you say.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      hsfougaris
                      wrote on last edited by
                      #10

                      that's the first thing i did

                      If you can't say what you mean, you'll never be able to mean what you say.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

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

                        Thanks!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved