Nominate our 2022 Qt Champions!

ComboBox filtering treeWidget ?

  • Hello,
    I got a combobox called " filters " with 2 items in it(blue and red)
    And a treewidget called tree1 with 2 columns ( name and color )
    What i want is that if i choose "red" as filter in my combobox, only item with "red" as color would be showed on my treewidget.

    i've heard i should use QSortFilterProxyModel but i have no idea how to do



  • This post is deleted!

  • Lifetime Qt Champion


    Please have some patience and allow 24 hours before bumping your own thread. This is a community forum with people not necessarily living in the same time zone as you.

    As for your question, you're likely looking to use QSortFilterProxyModel:: filterRegExp with the content of your combo box.

    • separate QTreeWidget in a QTreeView and a QStandardItemModel.
    • create a QSortFilterProxyModel and set the QStandardItemModel as its suorce model
    • connect currentTextChanged from the combobox to setFilterFixedString of QSortFilterProxyModel
    • if you need the filtering to be recursive then you can replace QSortFilterProxyModel with KRecursiveFilterProxyModel (I have no idea if/how you can use it in python though)

  • Sorry but could you just give me the code ?

    I tried:

    tree1 = QTreeWidget()
    sourceModel = MyItemModel()

    proxyModel = QSortFilterProxyModel()
    proxyModel.setFilterRegExp(QRegExp(".PNG", Qt.CaseIntensive,QRegExp.FixedString))


    I used PySide doc as example

  • Lifetime Qt Champion

    IIRC something like: