Results 1 to 2 of 2

Thread: QSqlRelationalDelegate displays foreign_key - id instead of name/value from Combobox

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalDelegate displays foreign_key - id instead of name/value from Combo

    short answer: QFilterSortProxyModel cant work using a proxy model.

    long answer: need to subclass QSqlRelationalDelegate to search in the proxy mode, the source model. and call the base function using the model and index if the sourceModel()

    1- need to find the first not-proxy model using the 'index', and the mapped model index related to proxy index 'index' and return the source model index and the index mapped from the proxy model
    Qt Code:
    1. std::tuple<QAbstractItemModel *, QModelIndex> findSourceModel(QModelIndex index) const
    2. {
    3. QAbstractItemModel * source_model = (QAbstractItemModel *)index.model();
    4. QModelIndex source_index = index;
    5.  
    6. while( source_model->inherits("QAbstractProxyModel") ){
    7. //qDebug() << "class name from proxy model " << source_model->metaObject()->className();
    8. source_index = static_cast<QAbstractProxyModel *>(source_model)->mapToSource(source_index);
    9. source_model = static_cast<QAbstractProxyModel *>(source_model)->sourceModel();
    10. }
    11.  
    12. return {source_model, source_index};
    13. }
    To copy to clipboard, switch view to plain text mode 

    and use the returned values whit the base class implementation of QSqlRelationalDelegate

    Qt Code:
    1. QWidget *QSqlRelationalProxyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. auto[source_model, source_index] = findSourceModel(index);
    4. return QSqlRelationalDelegate::createEditor(parent,option,source_index);
    5. }
    To copy to clipboard, switch view to plain text mode 

    NOTE:
    for a not proxy model. i refer to any subclass of QAbstractProxyModel, mainly a QSortProxyModelor QIdentityProxyModelsubclass

    I apologize for responding with a code in C++, but I haven't programmed in Python for a long time, it should be easy to convert it to Python.
    I hope it is useful to you
    Last edited by ecanela; 24th December 2023 at 05:25. Reason: updated contents

Similar Threads

  1. Replies: 0
    Last Post: 12th April 2017, 10:53
  2. QSqlRelationalDelegate
    By pippo42 in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2010, 12:05
  3. Replies: 1
    Last Post: 20th January 2010, 22:59
  4. QSqlRelationalDelegate (ComboBox) id on first show
    By janus in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2008, 13:58
  5. Error in qsqlrelationaldelegate.h?
    By brcain in forum Qt Programming
    Replies: 3
    Last Post: 28th August 2006, 12:05

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.