Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to select the role of a QSortFilterProxyModel from QML?
Forum Updated to NodeBB v4.3 + New Features

How to select the role of a QSortFilterProxyModel from QML?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qproxymodelrolefilter
2 Posts 2 Posters 193 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.
  • S Offline
    S Offline
    Saviz
    wrote on 29 Jan 2025, 00:24 last edited by
    #1

    I have a subclass of QSortFilterProxyModel to enable my ComboBox to filter records. Below is the code I currently have:

    filter.hpp:

    #ifndef FILTER_H
    #define FILTER_H
    
    #include <QObject>
    #include <QSortFilterProxyModel>
    
    class CustomProxyModel : public QSortFilterProxyModel
    {
        Q_OBJECT
        Q_PROPERTY(QString filterText READ filterText WRITE setFilterText NOTIFY filterTextChanged)
    
    public:
        explicit CustomProxyModel(QObject *parent = nullptr);
    
    private:
        QString m_filterText = "";
    
    signals:
        void filterTextChanged();
    
    protected:
        bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
    
    public:
        QString filterText() const;
        void setFilterText(const QString &text);
    };
    
    #endif // FILTER_H
    

    filter.cpp:

    QString CustomProxyModel::filterText() const
    {
        return m_filterText;
    }
    
    void CustomProxyModel::setFilterText(const QString &text)
    {
        if (m_filterText != text) {
            m_filterText = text;
            emit filterTextChanged();
            invalidateFilter(); // Reapply the filter
        }
    }
    
    bool CustomProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    {
        if (m_filterText.isEmpty()) {
            return true; // Accept all rows if no filter text is set
        }
    
        QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
        QVariant data = sourceModel()->data(index);
    
        qDebug() << "Data for filtering:" << data << "Type:" << data.typeName();
    
        return data.toString().contains(m_filterText, Qt::CaseInsensitive);
    }
    

    Additionally, here is the QML code I am using:

    ApplicationWindow {
      width: 300
      height: 500
      
      ListModel {
        id: myListModel
        // My elements have multiple roles:
        ListElement { id: 1; name: "Alice" }
        ListElement { id: 2; name: "Bob" }
        ListElement { id: 3; name: "Charlie" }
      }
    
      // Registered C++ type:
      CustomProxyModel {
        id: proxyModel
        sourceModel: myListModel
        // How to set role?
        filterString: "Alice"
      }
    
      ComboBox {
        anchors.fill: parent
        model: proxyModel
        textRole: "name"
      }
    }
    

    The problem I’m facing is that I cannot figure out how to set the textRole that the QSortFilterProxyModel should use for filtering. Currently, it filters based on the first role, which is id (a number). However, I want it to filter based on the name role (a string). How can I achieve this?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 29 Jan 2025, 07:08 last edited by
      #2

      Try something like this. Use the data method of index.

      bool CustomProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
      {
      const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent);
      // Some how get the the role you require.
      const QString filterElement = sourceIndex.data(role).toString();
      return(filterElement.toLower().startsWith(m_filterText));
      }

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0

      1/2

      29 Jan 2025, 00:24

      • Login

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