basicsortfiltermodel.py¶
import sys
from PySide6.QtCore import (QDate, QDateTime, QRegularExpression,
QSortFilterProxyModel, QTime, Qt)
from PySide6.QtGui import QStandardItemModel
from PySide6.QtWidgets import (QApplication, QCheckBox, QComboBox, QGridLayout,
QGroupBox, QHBoxLayout, QLabel, QLineEdit,
QTreeView, QVBoxLayout, QWidget)
REGULAR_EXPRESSION = 0
WILDCARD = 1
FIXED_STRING = 2
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.proxyModel = QSortFilterProxyModel()
self.proxyModel.setDynamicSortFilter(True)
self.sourceGroupBox = QGroupBox("Original Model")
self.proxyGroupBox = QGroupBox("Sorted/Filtered Model")
self.sourceView = QTreeView()
self.sourceView.setRootIsDecorated(False)
self.sourceView.setAlternatingRowColors(True)
self.proxyView = QTreeView()
self.proxyView.setRootIsDecorated(False)
self.proxyView.setAlternatingRowColors(True)
self.proxyView.setModel(self.proxyModel)
self.proxyView.setSortingEnabled(True)
self.sortCaseSensitivityCheckBox = QCheckBox("Case sensitive sorting")
self.filterCaseSensitivityCheckBox = QCheckBox("Case sensitive filter")
self.filterPatternLineEdit = QLineEdit()
self.filterPatternLineEdit.setClearButtonEnabled(True)
self.filterPatternLabel = QLabel("&Filter pattern:")
self.filterPatternLabel.setBuddy(self.filterPatternLineEdit)
self.filterSyntaxComboBox = QComboBox()
self.filterSyntaxComboBox.addItem("Regular expression",
REGULAR_EXPRESSION)
self.filterSyntaxComboBox.addItem("Wildcard",
WILDCARD)
self.filterSyntaxComboBox.addItem("Fixed string",
FIXED_STRING)
self.filterSyntaxLabel = QLabel("Filter &syntax:")
self.filterSyntaxLabel.setBuddy(self.filterSyntaxComboBox)
self.filterColumnComboBox = QComboBox()
self.filterColumnComboBox.addItem("Subject")
self.filterColumnComboBox.addItem("Sender")
self.filterColumnComboBox.addItem("Date")
self.filterColumnLabel = QLabel("Filter &column:")
self.filterColumnLabel.setBuddy(self.filterColumnComboBox)
self.filterPatternLineEdit.textChanged.connect(self.filterRegExpChanged)
self.filterSyntaxComboBox.currentIndexChanged.connect(self.filterRegExpChanged)
self.filterColumnComboBox.currentIndexChanged.connect(self.filterColumnChanged)
self.filterCaseSensitivityCheckBox.toggled.connect(self.filterRegExpChanged)
self.sortCaseSensitivityCheckBox.toggled.connect(self.sortChanged)
sourceLayout = QHBoxLayout()
sourceLayout.addWidget(self.sourceView)
self.sourceGroupBox.setLayout(sourceLayout)
proxyLayout = QGridLayout()
proxyLayout.addWidget(self.proxyView, 0, 0, 1, 3)
proxyLayout.addWidget(self.filterPatternLabel, 1, 0)
proxyLayout.addWidget(self.filterPatternLineEdit, 1, 1, 1, 2)
proxyLayout.addWidget(self.filterSyntaxLabel, 2, 0)
proxyLayout.addWidget(self.filterSyntaxComboBox, 2, 1, 1, 2)
proxyLayout.addWidget(self.filterColumnLabel, 3, 0)
proxyLayout.addWidget(self.filterColumnComboBox, 3, 1, 1, 2)
proxyLayout.addWidget(self.filterCaseSensitivityCheckBox, 4, 0, 1, 2)
proxyLayout.addWidget(self.sortCaseSensitivityCheckBox, 4, 2)
self.proxyGroupBox.setLayout(proxyLayout)
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.sourceGroupBox)
mainLayout.addWidget(self.proxyGroupBox)
self.setLayout(mainLayout)
self.setWindowTitle("Basic Sort/Filter Model")
self.resize(500, 450)
self.proxyView.sortByColumn(1, Qt.AscendingOrder)
self.filterColumnComboBox.setCurrentIndex(1)
self.filterPatternLineEdit.setText("Andy|Grace")
self.filterCaseSensitivityCheckBox.setChecked(True)
self.sortCaseSensitivityCheckBox.setChecked(True)
def setSourceModel(self, model):
self.proxyModel.setSourceModel(model)
self.sourceView.setModel(model)
def filterRegExpChanged(self):
syntax_nr = self.filterSyntaxComboBox.currentData()
pattern = self.filterPatternLineEdit.text()
if syntax_nr == WILDCARD:
pattern = QRegularExpression.wildcardToRegularExpression(pattern)
elif syntax_nr == FIXED_STRING:
pattern = QRegularExpression.escape(pattern)
regExp = QRegularExpression(pattern)
if not self.filterCaseSensitivityCheckBox.isChecked():
options = regExp.patternOptions()
options |= QRegularExpression.CaseInsensitiveOption
regExp.setPatternOptions(options)
self.proxyModel.setFilterRegularExpression(regExp)
def filterColumnChanged(self):
self.proxyModel.setFilterKeyColumn(self.filterColumnComboBox.currentIndex())
def sortChanged(self):
if self.sortCaseSensitivityCheckBox.isChecked():
caseSensitivity = Qt.CaseSensitive
else:
caseSensitivity = Qt.CaseInsensitive
self.proxyModel.setSortCaseSensitivity(caseSensitivity)
def addMail(model, subject, sender, date):
model.insertRow(0)
model.setData(model.index(0, 0), subject)
model.setData(model.index(0, 1), sender)
model.setData(model.index(0, 2), date)
def createMailModel(parent):
model = QStandardItemModel(0, 3, parent)
model.setHeaderData(0, Qt.Horizontal, "Subject")
model.setHeaderData(1, Qt.Horizontal, "Sender")
model.setHeaderData(2, Qt.Horizontal, "Date")
addMail(model, "Happy New Year!", "Grace K. <grace@software-inc.com>",
QDateTime(QDate(2006, 12, 31), QTime(17, 3)))
addMail(model, "Radically new concept", "Grace K. <grace@software-inc.com>",
QDateTime(QDate(2006, 12, 22), QTime(9, 44)))
addMail(model, "Accounts", "pascale@nospam.com",
QDateTime(QDate(2006, 12, 31), QTime(12, 50)))
addMail(model, "Expenses", "Joe Bloggs <joe@bloggs.com>",
QDateTime(QDate(2006, 12, 25), QTime(11, 39)))
addMail(model, "Re: Expenses", "Andy <andy@nospam.com>",
QDateTime(QDate(2007, 1, 2), QTime(16, 5)))
addMail(model, "Re: Accounts", "Joe Bloggs <joe@bloggs.com>",
QDateTime(QDate(2007, 1, 3), QTime(14, 18)))
addMail(model, "Re: Accounts", "Andy <andy@nospam.com>",
QDateTime(QDate(2007, 1, 3), QTime(14, 26)))
addMail(model, "Sports", "Linda Smith <linda.smith@nospam.com>",
QDateTime(QDate(2007, 1, 5), QTime(11, 33)))
addMail(model, "AW: Sports", "Rolf Newschweinstein <rolfn@nospam.com>",
QDateTime(QDate(2007, 1, 5), QTime(12, 0)))
addMail(model, "RE: Sports", "Petra Schmidt <petras@nospam.com>",
QDateTime(QDate(2007, 1, 5), QTime(12, 1)))
return model
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.setSourceModel(createMailModel(window))
window.show()
sys.exit(app.exec_())
© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.