python - 匹配 QTableWidget 中所有列中的行

标签 python python-2.7 pyqt

如果按下当前搜索按钮,则仅当与第二列中的值匹配时才显示相应行。

我想搜索所有列并输出所有匹配的行。

除了 item() 函数之外,我还尝试使用 rowCount() 和 ColumnCount(),但没有得到所需的结果。

    self.SearchEdit = QLineEdit()
    self.SearchEdit.setPlaceholderText("KeyWord")
    self.SearchButton = QPushButton("search")
    self.SearchButton.clicked.connect(self.OnSearch)

def OnSearch(self):
    if self.SearchEdit.text() == "":
        for i in range(0, tableWidget.rowCount()):
            tableWidget.setRowHidden(i, False)
        return
    for i in range(0, tableWidget.rowCount()):
        item = tableWidget.item(i,1)
        if (item is not None and item.data(QtCore.Qt.EditRole) == (self.SearchEdit.text())):
            tableWidget.setRowHidden(i, False)
        else:
            tableWidget.setRowHidden(i, True)

仅搜索一个特定列。我希望能够搜索所有列。

最佳答案

假设您想要隐藏没有任何项目与搜索匹配的行,则策略是使用一个标志来指示搜索是否找到匹配项:

def OnSearch(self):
    word = self.SearchEdit.text()
    if word:
        for i in range(tableWidget.rowCount()):
            match = False
            for j in range(tableWidget.columnCount()):
                item = tableWidget.item(i, j)
                if item is not None and item.text() == word:
                    match = True
                    break
            tableWidget.setRowHidden(i, not match)
    else:
        for i in range(tableWidget.rowCount()):
            tableWidget.setRowHidden(i, False)

关于python - 匹配 QTableWidget 中所有列中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57321230/

相关文章:

python - ~ 运算符在 Python 中有什么作用

python - PyQt:如何使用 QComboBox::findData() 查找元组?

QTreeWidget 激活项目信号

python - 为什么 asyncio 不总是使用执行器?

python - 通过十六进制字符串迭代

python - 如何使用列表解包将格式化字符串转换为 f 字符串?

python - 单元测试中的设置部分似乎被忽略

javascript - Django 将 JSON 数据传递给静态 getJSON/Javascript

c++ - 使 C++ 使用 Python 函数的输出

qt - 我在哪里可以获得 qt 默认 plastique qss 文件?