Results 1 to 3 of 3

Thread: Filter relation column options - QSqlRelationalDelegate? constrain or restrict option

  1. #1
    Join Date
    Sep 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Filter relation column options - QSqlRelationalDelegate? constrain or restrict option

    qt4.8

    I have a relational table model, with editable relations that work.

    The table is filtered, and the default relational delegate
    collects all possible values from the database as combo options.

    I would like to restrict the available options to either:

    a) the option set defined by the table's filter - i.e. what's available in the current table
    OR
    b) manually-generated list (of relations in the current table )

    either one will do.

    I attempted a specialized createEditor function to
    create an editor with a fixed/generated set of options.

    Qt Code:
    1. QWidget *TeamSelectDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem & option,
    3. const QModelIndex & index) const
    4. {
    5. Q_UNUSED(option);
    6. const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
    7. QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
    8. if (!childModel)
    9. return QItemDelegate::createEditor(parent, option, index);
    10.  
    11. QComboBox *combo = new QComboBox(parent);
    12. combo->setDuplicatesEnabled(false);
    13. combo->setModel(childModel);
    14. combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn()));
    15.  
    16. combo->clear(); // get rid of stuff from model,
    17. combo->addItems(*_relevant_teams); // and put in options from a subset ( generated list )
    18.  
    19. combo->installEventFilter(const_cast<TeamSelectDelegate *>(this));
    20. return combo;
    To copy to clipboard, switch view to plain text mode 

    "_relevant_teams" is a previously generated list of options.
    This did not work... it seemed to delete all items in the DB!

    Thanks for sharing your ideas.


    Cheers!
    mike
    Last edited by talkfig; 1st May 2012 at 23:18.

  2. #2
    Join Date
    Sep 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Filter relation column options - QSqlRelationalDelegate? constrain or restrict op

    Looks like no easy way to accomplish this -
    due to a lack of forethought perhaps.

    There is a function to get the model of the relations (to setup a filter) but,
    the "get" function itself triggers the SQL query to populate the model!

    So, my approach is to modify the sql/model/qsqlrelational bits to allow
    passing a filter string when setting up the relation like:

    Qt Code:
    1. Game::m_tmodel->setRelation(3, QSqlRelation("team", "id", "name", "division_id=3"));
    To copy to clipboard, switch view to plain text mode 

    Down the "populate()" function chain, a function actually looks for a WHERE condition but no way to setup conditions before the query happens.

    now, compiling the Qt sources...
    Last edited by talkfig; 3rd May 2012 at 23:54.

  3. #3
    Join Date
    Sep 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Filter relation column options - QSqlRelationalDelegate? constrain or restrict op

    Tweaking Qt sql-model code got desired results -

    Qt Code:
    1. diff -r ../../../QtSDK-orig/QtSources/4.8.1/src/sql/models/qsqlrelationaltablemodel.cpp ./src/sql/models/qsqlrelationaltablemodel.cpp
    2. 241a242
    3. > setFilter(relation->rel.where()); // This seems to work...
    4. diff -r ../../../QtSDK-orig/QtSources/4.8.1/src/sql/models/qsqlrelationaltablemodel.h ./src/sql/models/qsqlrelationaltablemodel.h
    5. 58,59c58,59
    6. < const QString &displayCol)
    7. < : tName(aTableName), iColumn(indexCol), dColumn(displayCol) {}
    8. ---
    9. > const QString &displayCol, const QString &where = QString())
    10. > : tName(aTableName), iColumn(indexCol), dColumn(displayCol), _where(where) {}
    11. 65a66,67
    12. > inline QString where() const
    13. > { return _where; }
    14. 69c71
    15. < QString tName, iColumn, dColumn;
    16. ---
    17. > QString tName, iColumn, dColumn, _where;
    To copy to clipboard, switch view to plain text mode 

    This allows specifying your WHERE clause to limit the association options for model relations - no special delegate is required.

    Qt Code:
    1. Game::m_tmodel->setRelation(4, QSqlRelation("team", "id", "name", "team.division_id IN (1,10,11,12)" ));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to set filter option in QTableWidget
    By johnMick in forum Newbie
    Replies: 7
    Last Post: 18th October 2011, 14:41
  2. Multiple axes with a relation
    By pkj in forum Qwt
    Replies: 1
    Last Post: 21st February 2011, 06:46
  3. QSqlRelationalDelegate
    By pippo42 in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2010, 12:05
  4. Tree different column counts by row- options?
    By LynneV in forum Qt Programming
    Replies: 0
    Last Post: 8th April 2010, 17:47
  5. QComboBox QSqlQueryModel & relation.
    By matheww in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2007, 04:56

Tags for this Thread

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.