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. Customize QSqlRelation to filter results
Qt 6.11 is out! See what's new in the release blog

Customize QSqlRelation to filter results

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 593 Views 2 Watching
  • 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    In Qt 6.8.0 I have a QSqlDatabase (SQLite) with two tables.

    Table "songs":

    QString sql = "CREATE TABLE songs \
        (id INT NOT NULL, \
         name TEXT, \
         enabled BOOLEAN, \
         duration INT NOT NULL, \
         extension TEXT NOT NULL, \
         PRIMARY KEY (id));";
    

    and table "events":

    QString sql = "CREATE TABLE events \
        (id INT NOT NULL, \
         idSong INT NOT NULL DEFAULT 0, \
         duration INT NOT NULL DEFAULT 1, \
         PRIMARY KEY (id), \
         FOREIGN KEY (idSong) REFERENCES songs(id));";
    

    I omitted other fields for clarity.
    Then I show the "events" table in a QTableView and I set it up in the following way:

    QSqlRelationalTableModel *_model = nullptr;
    
    // ...
    
    QSqlDatabase db = QSqlDatabase::database("mydb");
    _model = new QSqlRelationalTableModel(this, db);
    _model->setTable("events");
    _model->setEditStrategy(QSqlRelationalTableModel::OnRowChange);
    view->setSortingEnabled(false);
    
    connect(_model, &QSqlRelationalTableModel::beforeInsert, this, [=](QSqlRecord record)
    {
        emit rowInserted(_model->rowCount(), record);
    });
    
    connect(_model, &QSqlRelationalTableModel::beforeDelete, this, [=](int row)
    {
        emit rowRemoved(row, _model->record(row));
    });
    
    _model->setRelation(1, QSqlRelation("songs", "id", "name"));
    _model->select();
    

    Basically, I replicated the SQL relation at Qt-level.
    So far so good.

    In the QTableView I see a QComboBox that lists all the names of the table "songs".

    Now I want to customize (filter) this list, for example showing only the rows with the field "enabled" set to true.

    Is it possible to add such a constraint to the relation call?
    If is not possible, how can I do the same?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      I would check the implementation of QSqlRelationalDelegate, mimic it and add the filter there.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Mark81 has marked this topic as solved on

      • Login

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