Search code examples
c++qtqmlqsortfilterproxymodelqabstractlistmodel

Is possible set order by bool in QSortFilterProxyModel with setSortRole()?


Hello i have a class Filtro(in english is Filter) that is QSortFilterProxyModel , i have setted her sourceModel with myModel class. myModel class is a QAbstractListModel in this class i have a

QList<Tile> m_recipes; 

Tile class is this:

class Tile {

public :
    Tile (const QString &nameRecipe,const QString &color,const int &duration,const bool &userRecipe);
    QString nameRecipe()const;
    QString color() const;
    int duration() const;
    bool userRecipe() const;

private:

   QString m_nameRecipe;
   QString m_color;
   int     m_duration;
   bool    m_UserRecipe;
};

Now when i filter the list with the class Filtro, i want to show in qml before all the elements with m_UserRecipe == true and after all the elements with m_UserRecipe == false . So my question is : in QSortFilterProxyModel is possible set order with a role that rapresents a bool value ??

QHash<int, QByteArray> myModel::roleNames() const
{

    QHash <int,QByteArray> roles;
    roles[NameRecipe]="NameRecipe";
    roles[Color]="Color";
    roles[Duration]="Duration";
    roles[UserRecipe]="userRecipe";
    return roles;

}

filtro.h :

class Filtro : public QSortFilterProxyModel
{
    Q_OBJECT

public:
    Filtro(QObject* parent=0);
     ~Filtro();

    Q_INVOKABLE void setStringaFiltro(QString string);
    Q_PROPERTY(bool  showOnlyUserRic READ showOnlyUserRic WRITE setshowOnlyUserRic NOTIFY showOnlyUserRicChanged)
    Q_PROPERTY(QString string READ string WRITE setstring NOTIFY stringChanged)

public slots:

    void setshowOnlyUserRic(bool showOnlyUserRic);
    bool showOnlyUserRic() const;

    QString string() const;
    void setstring(QString string);

signals:
    void showOnlyUserRicChanged();
    void oggettiFiltChanged();

    void stringChanged();

private:

    bool m_showOnlyUserRic;
    int m_oggettiFilt;

    QString m_string;

protected:
    bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;

};

filtro.cpp

Filtro::Filtro(QObject *parent): QSortFilterProxyModel(parent)
      , m_showOnlyUserRic(false)
      ,m_oggettiFilt(0)
      ,m_string("")
    {
       this->setSourceModel(&myModel);

    }

    Filtro::~Filtro()
    {

    }

    void Filtro::setStringaFiltro(QString string)
    {
        this->setFilterCaseSensitivity(Qt::CaseInsensitive); // lo rendo case insensitive
        this->setFilterFixedString(string);
    }

    QString Filtro::string() const
    {
        return m_string;
    }

    void Filtro::setstring(QString string)
    {
        if (m_string == string)
            return;

        m_string = string;
        emit stringChanged();
        invalidateFilter();  // fa rivalutare il filtro e quindi entra di nuovo in filterAcceptsRows()
    }

    bool Filtro::showOnlyUserRic() const
    {
        return m_showOnlyUserRic;
    }

    void Filtro::setshowOnlyUserRic(bool showOnlyUserRic)
    {
        if (m_showOnlyUserRic == showOnlyUserRic)
            return;

        m_showOnlyUserRic = showOnlyUserRic;
        emit showOnlyUserRicChanged();

        invalidateFilter();  // fa rivalutare il filtro e quindi entra di nuovo in filterAcceptsRows()

    }



    bool Filtro::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
    {


        QRegExp regEx("*"+string()+"*");
        regEx.setPatternSyntax(QRegExp::Wildcard);
        regEx.setCaseSensitivity(Qt::CaseInsensitive);

        if(showOnlyUserRic()==true) {
         // se devo visualizzare solo
         QModelIndex ricUtente = sourceModel()->index(source_row,0, source_parent); // vado a leggere singolarmente ogni riga del modello
         QString stringaConfronto=sourceModel()->data(ricUtente,modello::NomeRicetta).toString();

         if(sourceModel()->data(ricUtente,modello::RicettaUtente)==true && stringaConfronto.contains(regEx)==true)
         {
             // se è ricetta utente
             return true;
         }
         else{

             return false;
         }

       }
        else{

            QModelIndex ricUtente = sourceModel()->index(source_row,0, source_parent); // vado a leggere singolarmente ogni riga del modello
            QString stringaConfronto=sourceModel()->data(ricUtente,modello::NomeRicetta).toString();
            if(stringaConfronto.contains(regEx))
                return true;
    //        if(sourceModel()->data(ricUtente,modello::NomeRicetta)== string() )  //confronto il roles ricetta utene x filtrare il
    //        {
    //            return true;
    //        }
            return false;
   }
}

Solution

  • If you want to have an order of that type you just have to overwrite the lessThan method:

    bool Filtro::lessThan(const QModelIndex &left, const QModelIndex &right) const
    {
        bool leftData = sourceModel()->data(left, modello::UserRecipe).toBool();
        bool rightData = sourceModel()->data(right,  modello::UserRecipe).toBool();
    
        if(leftData != rightData){
            return leftData;
        }
        else
            return QSortFilterProxyModel::lessThan(left, right);
    }
    

    Update:

    you must call sort(0)(the number does not matter since it is made for a multi-column model, but in your case it is not.), also enable the dynamicSortFilter, and for the last one in your case, you must set the sorting role to modello::NomeRicetta:

    Filtro::Filtro(QObject *parent): QSortFilterProxyModel(parent)
      , m_showOnlyUserRic(false)
      ,m_oggettiFilt(0)
      ,m_string("")
    {
        setFilterRole(modello::RicettaUtente);
        setSortRole(modello::NomeRicetta);
        setDynamicSortFilter(true);
        sort(0);
    }
    

    The complete example can be found on this link