我在项目中使用QTableWidget。但是QTableWidget中没有搜索功能。

How to set filter option in qtablewidget

QTableView允许搜索。但是这一次QTableView,非英语字符未正确排序。 QTableWidget排序很棒。我想使用QTableView。如何在QTableView中修复非英语字符排序。

python - PyQt QTableView非英语字符排序问题-LMLPHP

QTableWidget代码

class GuiQTableWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle("QTableWidget")

        v_box = QVBoxLayout()
        v_box.addWidget(self.prepare_table())
        self.setLayout(v_box)

        self.setMinimumHeight(750)
        self.setFixedWidth(self.width())

    def prepare_table(self):
        letter_list = [ 'A','B','C','Ç','G','Ğ','I','I','O','Ö','P','S','Ş','U','Ü','Y','Z',
                           'a','b','c','ç','g','ğ','ı','i','o','ö','p','s','ş','u','ü','y','z' ]

        table = QTableWidget()
        table.setColumnCount(1)
        table.setHorizontalHeaderLabels(["Letter"])
        table.setRowCount(len(letter_list))

        for row, letter in enumerate(letter_list):
            table.setItem(row, 0, QTableWidgetItem(letter))

        h_header = table.horizontalHeader()
        h_header.setSectionResizeMode(QHeaderView.Stretch)

        table.setSortingEnabled(True)
        table.setAlternatingRowColors(True)

        return table


QTableView

class GuilQTableView(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

        # sort
        self.h_header.sortIndicatorChanged.connect(self.sort)
        # filter
        self.line_search.textChanged.connect(self.filter)

    def init_ui(self):
        self.setWindowTitle("QTableView")

        self.table = QTableView()
        self.h_header = self.table.horizontalHeader()
        self.model = QStandardItemModel()
        self.proxyFilter = QSortFilterProxyModel()

        self.line_search = QLineEdit()
        lbl_search = QLabel("Letter Search")
        h_search = QHBoxLayout()
        h_search.addWidget(lbl_search)
        h_search.addWidget(self.line_search)

        v_box = QVBoxLayout()
        v_box.addLayout(h_search)
        v_box.addWidget(self.prepare_table(self.table, self.model, self.proxyFilter, self.h_header))
        self.setLayout(v_box)

        self.setMinimumHeight(750)
        self.setFixedWidth(self.width())

    def prepare_table(self, table, model, proxyFilter, h_header):
        letter_list = [ 'A','B','C','Ç','G','Ğ','I','I','O','Ö','P','S','Ş','U','Ü','Y','Z',
                           'a','b','c','ç','g','ğ','ı','i','o','ö','p','s','ş','u','ü','y','z' ]

        model.setColumnCount(1)
        model.setRowCount(len(letter_list))
        model.setHorizontalHeaderLabels(['Letter'])

        for row, letter in enumerate(letter_list):
            model.setItem(row, 0, QStandardItem(letter))

        proxyFilter.setSourceModel(model)
        proxyFilter.setFilterKeyColumn(0)
        proxyFilter.setSortCaseSensitivity(Qt.CaseSensitive)

        table.setModel(proxyFilter)
        table.setAlternatingRowColors(True)
        table.setSelectionBehavior(QAbstractItemView.SelectRows)
        table.setSelectionMode(QAbstractItemView.ExtendedSelection)

        h_header.setSectionResizeMode(QHeaderView.Stretch)

        return table

    def sort(self, sortOrder):
        if sortOrder == Qt.AscendingOrder:
            self.model.sort(0, Qt.AscendingOrder)
        else:
            self.model.sort(0, Qt.DescendingOrder)

        self.h_header.setSortIndicatorShown(True)

    def filter(self, text):
        regExp = QRegExp(text, Qt.CaseInsensitive, QRegExp.Wildcard)
        self.proxyFilter.setFilterRegExp(regExp)


QTableWidget正确排序。 QTableView在列表的底部列出了非英语字符(Ç,G,Ğ,İ,Ö,Ş,Ü,ç,ğ,ı,ö,ş,ü)。
如何在QTableView中修复非英语字符排序。

最佳答案

QTableWidget的情况下,使用operator <QTableWidgetItem,在这种情况下,使用QString作为字符的位置作为参考,而在QSortFilterProxyModel情况下,使用不将位置作为字符的python字符串。参考,这就是为什么看到这种差异的原因,解决方案是覆盖lessThan()QSortFilterProxyModel方法

另一方面,由于sortIndicatorChanged在内部为模型提供建议,因此不必使用QTableView信号。

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class SortFilterProxyModel(QSortFilterProxyModel):
    def lessThan(self, left, right):
        leftData = self.sourceModel().data(left)
        rightData = self.sourceModel().data(right)
        return QTableWidgetItem(leftData) < QTableWidgetItem(rightData)

class GuilQTableView(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        # filter
        self.line_search.textChanged.connect(self.filter)

    def init_ui(self):
        self.setWindowTitle("QTableView")

        self.table = QTableView()
        self.table.setSortingEnabled(True)
        self.h_header = self.table.horizontalHeader()
        self.model = QStandardItemModel()
        self.proxyFilter = SortFilterProxyModel()

        self.line_search = QLineEdit()
        lbl_search = QLabel("Letter Search")
        h_search = QHBoxLayout()
        h_search.addWidget(lbl_search)
        h_search.addWidget(self.line_search)

        v_box = QVBoxLayout()
        v_box.addLayout(h_search)
        v_box.addWidget(self.prepare_table(self.table, self.model, self.proxyFilter, self.h_header))
        self.setLayout(v_box)

        self.setMinimumHeight(750)
        self.setFixedWidth(self.width())

    def prepare_table(self, table, model, proxyFilter, h_header):
        letter_list = [ 'A','B','C','Ç','G','Ğ','I','I','O','Ö','P','S','Ş','U','Ü','Y','Z',
                           'a','b','c','ç','g','ğ','ı','i','o','ö','p','s','ş','u','ü','y','z' ]

        model.setColumnCount(1)
        model.setRowCount(len(letter_list))
        model.setHorizontalHeaderLabels(['Letter'])

        for row, letter in enumerate(letter_list):
            model.setItem(row, 0, QStandardItem(letter))

        proxyFilter.setSourceModel(model)
        proxyFilter.setFilterKeyColumn(0)
        proxyFilter.setSortCaseSensitivity(Qt.CaseSensitive)

        table.setModel(proxyFilter)
        table.setAlternatingRowColors(True)
        table.setSelectionBehavior(QAbstractItemView.SelectRows)
        table.setSelectionMode(QAbstractItemView.ExtendedSelection)

        h_header.setSectionResizeMode(QHeaderView.Stretch)

        return table

    def filter(self, text):
        regExp = QRegExp(text, Qt.CaseInsensitive, QRegExp.Wildcard)
        self.proxyFilter.setFilterRegExp(regExp)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = GuilQTableView()

    w.show()
    sys.exit(app.exec_())

关于python - PyQt QTableView非英语字符排序问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51271956/

10-11 19:07