1

您好,我有一个类 Filtro(英文是 Filter),它是 QSortFilterProxyModel ,我已将她的 sourceModel 设置为 myModel 类。myModel 类是这个类中的一个 QAbstractListModel 我有一个

QList<Tile> m_recipes; 

瓦片类是这样的:

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;
};

现在,当我使用类 Filtro 过滤列表时,我想在 m_UserRecipe == true 的所有元素之前和 m_UserRecipe == false 的所有元素之后显示在 qml 中。所以我的问题是:在 QSortFilterProxyModel 中是否可以设置具有表示布尔值的角色的顺序?

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

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

}

过滤器.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;

};

过滤器.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;
   }
}
4

1 回答 1

0

如果您想获得该类型的订单,您只需覆盖该lessThan方法:

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);
}

更新:

您必须调用sort(0)(编号无关紧要,因为它是为多列模型制作的,但在您的情况下不是。),还启用dynamicSortFilter, 对于您的情况的最后一个,您必须将排序角色设置为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);
}

完整的例子可以在这个链接上找到

于 2018-02-17T15:57:48.570 回答