Results 1 to 1 of 1

Thread: Editable QSqlRelationalTableModel - How to apply filter on Delegate in TableView

  1. #1
    Join Date
    Aug 2018
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Editable QSqlRelationalTableModel - How to apply filter on Delegate in TableView

    Hello,

    I have a problem that I'm not sure how to solve:

    I use a local file based SQLite DB in my Qt Application. I have 3 static relational tables that are created like this (simplified):

    Qt Code:
    1. QSqlQuery createProjekteTable("CREATE TABLE IF NOT EXISTS projekte (id INTEGER PRIMARY KEY, name TEXT)");
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QSqlQuery createSubProjekteTable("CREATE TABLE IF NOT EXISTS subProjekte (id INTEGER PRIMARY KEY, name TEXT, p_id INTEGER)");
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QSqlQuery createTasksTable("CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY, name TEXT)");
    To copy to clipboard, switch view to plain text mode 

    Important: The table "subProjekte" has a foreign key " p_id" that is related to the "id" primary key of the table "projekte". With other words every subProject belongs to a Project.

    Then I created a editable table that has relations to the above tables:

    Qt Code:
    1. QSqlQuery createControllingTable("CREATE TABLE IF NOT EXISTS controlling (id INTEGER PRIMARY KEY, projekt_id INTEGER, sub_id INTEGER, task_id INTEGER, minuten INTEGER, date DATE, text TEXT, submitted BOOLEAN)");
    To copy to clipboard, switch view to plain text mode 

    I want to be able to edit this table using a QTableView with delegates. Therefore I use a QSqlRelationalTableModel and define the relations like this:

    Qt Code:
    1. // join name of subProjekt in controlling at sub_id
    2.  
    3. subProjektIndex = relControllingModel->fieldIndex("sub_id");
    4. relControllingModel->setRelation(subProjektIndex, QSqlRelation("subProjekte", "id", "name"));
    5.  
    6. // join name of projekt in controlling at projekt_id
    7.  
    8. projektIndex = relControllingModel->fieldIndex("projekt_id");
    9. relControllingModel->setRelation(projektIndex, QSqlRelation("projekte", "id", "name"));
    10.  
    11. // join name of task in controlling at task_id
    12.  
    13. taskIndex = relControllingModel->fieldIndex("task_id");
    14. relControllingModel->setRelation(taskIndex, QSqlRelation("tasks", "id", "name"));
    15.  
    16. relControllingModel->select();
    To copy to clipboard, switch view to plain text mode 

    Then in my MainWindow i have the table view that shall be used as an editor for the controlling table:

    Qt Code:
    1. ui->tableView->setModel(model);
    2. ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));
    To copy to clipboard, switch view to plain text mode 

    This works great. I can double click the Projekt / Sub Projekt / Task fields in my View and it shows a dropdown with the records from the foreign tables.

    My Problem is that the Dropdown for SubProjekt shows ALL records from the table "subProjekte". But as I said above, theres an additional relation: The dropdown should only show records from subProjekte WHERE subProjekte.projekt_id = projekte.id (for the project that is currently selected in the dropdown delegate).

    How do i solve this? I need to apply a filter on the subProjekte dropdown delegate. Do i solve this problem on the relational model? Or on the view? Or can i kind of specify another relation for the controlling table?

    Any hints? Thank you very much in advance!
    Last edited by daniel.ste; 14th August 2018 at 12:04.

Similar Threads

  1. Custom editable TableView
    By KeineAhnung in forum Qt Quick
    Replies: 0
    Last Post: 6th March 2016, 21:24
  2. how to apply filter funcitonality for QtreeWidgetItem
    By vkaushik in forum Qt Programming
    Replies: 0
    Last Post: 5th September 2014, 20:53
  3. QSqlRelationalTableModel and editable QComboBox
    By cia.michele in forum Qt Programming
    Replies: 1
    Last Post: 9th October 2012, 12:01
  4. How to apply TreeView look & feel to a delegate?
    By estecb in forum Qt Programming
    Replies: 5
    Last Post: 15th March 2012, 12:01
  5. Replies: 0
    Last Post: 15th September 2010, 12:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.