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. QSqlRelationalTableModel and setFilter
Forum Updated to NodeBB v4.3 + New Features

QSqlRelationalTableModel and setFilter

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 424 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.
  • W Offline
    W Offline
    wolfgang.hammes
    wrote on last edited by
    #1

    Hello,

    Is it possible to set a filter for a QSqlRelationalTableModel?

    When I try

        model = new QSqlRelationalTableModel(this);
        model->setTable(QStringLiteral("receiver"));
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        addressIndex = model->fieldIndex(QStringLiteral("address_id"));
        model->setRelation(addressIndex, QSqlRelation(QStringLiteral("address"),
                                                      QStringLiteral("id"),
                                                      QStringLiteral("address")));
        QString idString = QString::number(id);
        QString filter = QString(QStringLiteral("id = %1")).arg(idString);
        model->setFilter(filter);
        model->select();
    

    I get zero rows. Trying 'the same' with a QSqlTableModel

        model = new QSqlTableModel(this);
        model->setTable(QStringLiteral("receiver"));
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        //addressIndex = model->fieldIndex(QStringLiteral("address_id"));
        //model->setRelation(addressIndex, QSqlRelation(QStringLiteral("address"),
        //                                              QStringLiteral("id"),
        //                                              QStringLiteral("address")));
        QString idString = QString::number(id);
        QString filter = QString(QStringLiteral("id = %1")).arg(idString);
        model->setFilter(filter);
        model->select();
    

    I am working on it for hours but find no solution. Maybe I have to use another order for the relational model?

    JonBJ 1 Reply Last reply
    0
    • W wolfgang.hammes

      Hello,

      Is it possible to set a filter for a QSqlRelationalTableModel?

      When I try

          model = new QSqlRelationalTableModel(this);
          model->setTable(QStringLiteral("receiver"));
          model->setEditStrategy(QSqlTableModel::OnManualSubmit);
          addressIndex = model->fieldIndex(QStringLiteral("address_id"));
          model->setRelation(addressIndex, QSqlRelation(QStringLiteral("address"),
                                                        QStringLiteral("id"),
                                                        QStringLiteral("address")));
          QString idString = QString::number(id);
          QString filter = QString(QStringLiteral("id = %1")).arg(idString);
          model->setFilter(filter);
          model->select();
      

      I get zero rows. Trying 'the same' with a QSqlTableModel

          model = new QSqlTableModel(this);
          model->setTable(QStringLiteral("receiver"));
          model->setEditStrategy(QSqlTableModel::OnManualSubmit);
          //addressIndex = model->fieldIndex(QStringLiteral("address_id"));
          //model->setRelation(addressIndex, QSqlRelation(QStringLiteral("address"),
          //                                              QStringLiteral("id"),
          //                                              QStringLiteral("address")));
          QString idString = QString::number(id);
          QString filter = QString(QStringLiteral("id = %1")).arg(idString);
          model->setFilter(filter);
          model->select();
      

      I am working on it for hours but find no solution. Maybe I have to use another order for the relational model?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @wolfgang-hammes
      This gets complicated! First of all, do you have a column named id in both tables? Where does your (unused) address_id come into it?

      You may want to read through very old https://forum.qt.io/topic/2213/qsqlrelationaltablemodel-and-setfilter-difficulties-possible-bug, and also the description about "aliased" columns, in https://doc.qt.io/qt-6/qsqlrelationaltablemodel.html#details.

      Start by printing out lastQuery() (after SELECT has been issued) so we can understand what query it is generating.

      W 1 Reply Last reply
      1
      • JonBJ JonB

        @wolfgang-hammes
        This gets complicated! First of all, do you have a column named id in both tables? Where does your (unused) address_id come into it?

        You may want to read through very old https://forum.qt.io/topic/2213/qsqlrelationaltablemodel-and-setfilter-difficulties-possible-bug, and also the description about "aliased" columns, in https://doc.qt.io/qt-6/qsqlrelationaltablemodel.html#details.

        Start by printing out lastQuery() (after SELECT has been issued) so we can understand what query it is generating.

        W Offline
        W Offline
        wolfgang.hammes
        wrote on last edited by
        #3

        @JonB

        Thank you very much. The last query was

         "SELECT receiver.`id`,relTblAl_1.address,receiver.`name`,receiver.`name_ex`,receiver.`street`,receiver.`zip_code`,receiver.`city` FROM receiver,address relTblAl_1 WHERE (receiver.`address_id`=relTblAl_1.id) AND (id = 1)" 
        

        and

        QString filter = QString(QStringLiteral("receiver.`id` = %1")).arg(idString);
        

        is getting the right row.

        JonBJ 1 Reply Last reply
        0
        • W wolfgang.hammes has marked this topic as solved on
        • W wolfgang.hammes

          @JonB

          Thank you very much. The last query was

           "SELECT receiver.`id`,relTblAl_1.address,receiver.`name`,receiver.`name_ex`,receiver.`street`,receiver.`zip_code`,receiver.`city` FROM receiver,address relTblAl_1 WHERE (receiver.`address_id`=relTblAl_1.id) AND (id = 1)" 
          

          and

          QString filter = QString(QStringLiteral("receiver.`id` = %1")).arg(idString);
          

          is getting the right row.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @wolfgang-hammes
          I believe that means you are working now (right?). See how useful lastQuery() is, especially with QSqlRelationalTableModel to understand the JOIN and alias names?

          W 1 Reply Last reply
          0
          • JonBJ JonB

            @wolfgang-hammes
            I believe that means you are working now (right?). See how useful lastQuery() is, especially with QSqlRelationalTableModel to understand the JOIN and alias names?

            W Offline
            W Offline
            wolfgang.hammes
            wrote on last edited by
            #5

            @JonB

            Oh yes. It can really help.

            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