Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Filtring TreeView QML



  • Hi everyone, so in my project I intend to do searching method to filter Treeview after filling a text field but i cant catch the way to do it, this is the code

    import QtQuick 2.15
    import QtQuick.Layouts 1.14
    import QtQuick.Controls 1.4
    
    ApplicationWindow{
    
    
    
            id:tree
    
            visible: true
            width: 800
            height: 800
            title: qsTr("Tree example")
    
    
            GroupBox{
                 anchors.fill: parent
    
    
    
    
            TreeView {
                id:treee
                anchors.fill: parent
    
    
                model: myModel
                 TableViewColumn {
                    title: "Name"
                    role: "display"
                    width: 200
                }
    
    
                onDoubleClicked: {
    
                }
                onPressAndHold: {
    
                }
            }
            MessageDialog {
                  id: messageDialog
                  title: "Info"
              }
    
    
    
            }
    
        }
    
    

    cpp code is below :

    #ifndef TREEVIEWMODEL_H
    #define TREEVIEWMODEL_H
    
    #include <QObject>
    #include<QStandardItemModel>
    
    
    class TreeViewModel : public QStandardItemModel
    {
        Q_OBJECT
    public:
         TreeViewModel(QObject *parent = nullptr);
    
    signals:
    
    public slots:
         void readDataBase();
    
    
    };
    
    #endif // TREEVIEWMODEL_H
    
    #include "treeviewmodel.h"
    #include <QStandardItem>
    
    
    TreeViewModel::TreeViewModel(QObject *parent) : QStandardItemModel{parent}
    {
        CanConverter canConverter;
    
        //connect(canConverter.getInstance(),SIGNAL(dataLoaded()),this,SLOT(readDataBase()));
        setColumnCount(1);
        QStandardItem *rootItem=invisibleRootItem();
    
        QStandardItem *DataBase1= new QStandardItem;
        QStandardItem *DataBase2 =new QStandardItem;
        QStandardItem *DataBase3= new QStandardItem;
    
    
        QStandardItem *Trame1=new QStandardItem;
        QStandardItem *Trame2=new QStandardItem;
        QStandardItem *Trame3=new QStandardItem;
        QStandardItem *Trame4=new QStandardItem;
        QStandardItem *Trame5=new QStandardItem;
        QStandardItem *Trame6=new QStandardItem;
        //QStandardItem *Trame7=new QStandardItem;
    
    
        DataBase1->setText("base de donné 1");
        DataBase1->setText("base de donné 2");
        DataBase1->setText("base de donné 3");
    
        Trame1->setText("Trame1");
        Trame2->setText("Trame2");
        Trame3->setText("Trame3");
        Trame4->setText("Trame4");
        Trame5->setText("Trame5");
        Trame6->setText("Trame6");
    
    
    
        rootItem->appendRow(DataBase1);
        rootItem->appendRow(DataBase2);
        rootItem->appendRow(DataBase3);
    
    
        DataBase1->appendRow(Trame1);
        DataBase1->appendRow(Trame2);
    
        DataBase2->appendRow(Trame3);
        DataBase2->appendRow(Trame4);
    
        DataBase3->appendRow(Trame5);
        DataBase3->appendRow(Trame6);
    
    
    
    }
    


  • Pleaase is there any solution for that





  • @fcarney , thans for your reply, but when i use it, implementing the code below of filtring
    b2e24dc0-00d6-445b-a720-93ff86457a0d-image.png

    so I get the problem when filtring, and the childrens of nodes wasn't shown, if i don't exapnd the nodes
    69c2a2e0-ca40-4a66-bf3e-7a6475e26fae-image.png
    pleaase is there any solution, it's been 4 days I m struglling with it



  • Is there any solution for that please, I don't Know if I m doing wrong while implementing this filterAcceptRow(), is the syntax right?



  • Hi, evreyOne, so I tried also, the findItem() method but it'is not working well, and I want to use the QProxySortFilterModel, implemeting the method filterAcceptRow(), I did it but also work with some problems, I think that I don't implement it right pleease need help


  • Lifetime Qt Champion

    Hi,

    You are filtering a tree model, you have to allow for the child items to pass your filter.



  • @SGaist thanks a lots for your reply, bu how to do it?