gpt4 book ai didi

python - QTreeview Pyside 中的搜索过滤器

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:10 25 4
gpt4 key购买 nike

我下面有一个工作示例,它添加了搜索过滤器功能。我觉得我的做法太过分了。它是根据我在网上找到的项目拼凑而成的。我真的必须创建整个 SearchProxyFilter 才能对我的项目列表进行简单的字符串过滤搜索吗?

我有一种感觉,我做错了什么,或者做了超出我需要的事情。如有任何建议,我们将不胜感激。

enter image description here

#######
# Imports
#######
import sys
import os
from PySide import QtGui, QtCore

#######
# Main
#######
class SortModel(QtGui.QSortFilterProxyModel):

def __init__(self, *args, **kwargs):
super(SortModel, self).__init__(*args, **kwargs)
self._pattern = ''

@property
def pattern(self):
return self._pattern

def filterAcceptsRow(self, sourceRow, sourceParent):
if not self.pattern:
return True

sm = self.sourceModel()
modelIdx = sm.index(sourceRow, 0)
if modelIdx.isValid():
txt = modelIdx.data(role=QtCore.Qt.DisplayRole)
if not self.pattern.lower() in txt.lower():
return False

return True

def lessThan(self, left, right):
leftData = self.sourceModel().data(left)
rightData = self.sourceModel().data(right)

if leftData:
leftData = leftData.lower()
if rightData:
rightData = rightData.lower()

return leftData < rightData

class UserBrowser(QtGui.QDialog):

def __init__(self, parent=None):
super(UserBrowser, self).__init__(parent)
self.resize(300, 500)
self.setWindowTitle('Users')
self.setModal(True)
self.initUI()

def initUI(self):
self.results = ''
self.filepath = ''

self.ui_search = QtGui.QLineEdit()
self.ui_search.setPlaceholderText('Search...')

self.source_model = QtGui.QStandardItemModel()
self.user_model = SortModel(self)
self.user_model.setSourceModel(self.source_model)
self.user_model.setDynamicSortFilter(True)
self.user_model.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)

self.ui_items = QtGui.QTreeView()
self.ui_items.setModel(self.user_model)
self.ui_items.setAlternatingRowColors(False)
self.ui_items.setSortingEnabled(True)
self.ui_items.sortByColumn(0, QtCore.Qt.AscendingOrder)
self.ui_items.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.ui_items.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
self.ui_items.setHeaderHidden(True)
self.ui_items.setRootIsDecorated(False)

gdl = QtGui.QGridLayout()
gdl.setContentsMargins(10, 10, 10, 10)
gdl.addWidget(self.ui_search, 0, 0)
gdl.addWidget(self.ui_items, 1, 0)
self.setLayout(gdl)
self.create_model()

self.ui_search.textChanged.connect(self.changed_text)

# Methods
def create_model(self):
model = self.source_model
model.clear()
model.setHorizontalHeaderLabels(['Names'])

users = ['kevin','Marry','Doug','Leslie','Michelle','John']
for x in users:
item = QtGui.QStandardItem()
item.setData(x , role=QtCore.Qt.DisplayRole)
model.appendRow(item)

self.ui_items.sortByColumn(0, QtCore.Qt.AscendingOrder)

# Actions
def changed_text(self, text):
self.user_model._pattern = text
self.user_model.invalidateFilter()

def main():
app = QtGui.QApplication(sys.argv)
ex = UserBrowser()
ex.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

最佳答案

使用 QCompleter 可以更简单地完成此操作。 :

class UserBrowser(QtGui.QDialog):
...
def initUI(self):
...
self.completer = QtGui.QCompleter(self)
self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.completer.setModelSorting(
QtGui.QCompleter.CaseInsensitivelySortedModel)

self.ui_items = QtGui.QTreeView()
self.ui_items.setModel(self.completer.completionModel())
...
self.create_model()
...
self.ui_search.textChanged.connect(self.completer.setCompletionPrefix)

# Methods
def create_model(self):
users = ['kevin','Marry','Doug','Leslie','Michelle','John']
users.sort(key=str.lower)
self.completer.setModel(QtGui.QStringListModel(users))

关于python - QTreeview Pyside 中的搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46689062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com