首页 > 解决方案 > Use finditem() only on one Column

问题描述

I have a QTableWidget populated with QtableWidgetItems.

I want a searchbar, where I can type in and as Response the Table should be refreshing and only showing the items that match partially with the string in the search field.

Im using finditem for that, but i want that only one column is used for the search. How can I do that?

标签: pythonpyqtpyqt5qtablewidgetqtablewidgetitem

解决方案


Iterate the table manually.

columnOfInterest = 1 # or whatever
valueOfInterest = "foo" 
for rowIndex in range(self.myTable.rowCount()):
    twItem = self.myTable.item(rowIndex, columnOfInterest)
    if twItem.text() == valueOfInterest:
        self.myTable.setRowHidden(rowIndex, False)
    else:
        self.myTable.setRowHidden(rowIndex, True)

You will have to implement better matching criteria. You can use string functions like str.find and str.startswith and others if you want to do it yourself.


推荐阅读