PyQt QTableView非英文字符排序问题
PyQt QTableView non-english character sorting issue
我在一个项目中使用了QTableWidget。但是QTableWidget中没有搜索功能。
How to set filter option in qtablewidget
QTableView 允许搜索。但是这次QTableView,非英文字符排序不正确。 QTableWidget 排序很棒。我想使用 QTableView。 QTableView中非英文字符排序如何解决
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
的情况下,使用QTableWidgetItem
的operator <
,在这种情况下使用QString作为参考字符的位置和QSortFilterProxyModel的情况使用不以地区为参考的 python 字符串,这就是为什么您会看到这种差异,解决方案是覆盖 QSortFilterProxyModel
的 lessThan()
方法
另一方面,没有必要使用 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_())
我在一个项目中使用了QTableWidget。但是QTableWidget中没有搜索功能。
How to set filter option in qtablewidget
QTableView 允许搜索。但是这次QTableView,非英文字符排序不正确。 QTableWidget 排序很棒。我想使用 QTableView。 QTableView中非英文字符排序如何解决
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
的情况下,使用QTableWidgetItem
的operator <
,在这种情况下使用QString作为参考字符的位置和QSortFilterProxyModel的情况使用不以地区为参考的 python 字符串,这就是为什么您会看到这种差异,解决方案是覆盖 QSortFilterProxyModel
lessThan()
方法
另一方面,没有必要使用 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_())