我即将分析以下可执行代码。好吧,我想要隐藏一个特殊的QTreeView列。在这种情况下,我阅读了Qt的文档,在那里我找到了setColumnHidden()-method
。 Qt说:
如果hide为true,则隐藏给定列
请查看Window()-class
。您看到我会尝试隐藏self.proxyView
的第一列,但它仍然可见。我做错了什么?
#!/usr/bin/env python
# This is only needed for Python v2 but is harmless for Python v3.
import sip
sip.setapi('QVariant', 2)
from PyQt4 import QtCore, QtGui
class MySortFilterProxyModel(QtGui.QSortFilterProxyModel):
def __init__(self, parent=None):
QtGui.QSortFilterProxyModel.__init__(self, parent)
#super(MySortFilterProxyModel, self).__init__(parent)
self.minDate = QtCore.QDate()
self.maxDate = QtCore.QDate()
def setFilterMinimumDate(self, date):
self.minDate = date
self.invalidateFilter()
def filterMinimumDate(self):
return self.minDate
def setFilterMaximumDate(self, date):
self.maxDate = date
self.invalidateFilter()
def filterMaximumDate(self):
return self.maxDate
def filterAcceptsRow(self, sourceRow, sourceParent):
#print "filterAcceptsRow is called"
#print "sourceRow", sourceRow
#print "sourceParent", sourceParent
index0 = self.sourceModel().index(sourceRow, 0, sourceParent)
index1 = self.sourceModel().index(sourceRow, 1, sourceParent)
index2 = self.sourceModel().index(sourceRow, 2, sourceParent)
return ( (self.filterRegExp().indexIn(str(self.sourceModel().data(index0))) >= 0
or self.filterRegExp().indexIn(self.sourceModel().data(index1)) >= 0)
and self.dateInRange(self.sourceModel().data(index2)))
def lessThan(self, left, right):
leftData = self.sourceModel().data(left)
rightData = self.sourceModel().data(right)
if not isinstance(leftData, QtCore.QDate):
emailPattern = QtCore.QRegExp("([\\w\\.]*@[\\w\\.]*)")
if left.column() == 1 and emailPattern.indexIn(leftData) != -1:
leftData = emailPattern.cap(1)
if right.column() == 1 and emailPattern.indexIn(rightData) != -1:
rightData = emailPattern.cap(1)
return leftData < rightData
def dateInRange(self, date):
if isinstance(date, QtCore.QDateTime):
date = date.date()
return ( (not self.minDate.isValid() or date >= self.minDate)
and (not self.maxDate.isValid() or date <= self.maxDate))
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.proxyModel = MySortFilterProxyModel(self)
self.proxyModel.setDynamicSortFilter(True)
self.model = QtGui.QStandardItemModel(0, 3, self)
self.filterPatternLineEdit = QtGui.QLineEdit()
self.filterPatternLineEdit.setText("Grace|Sports")
self.filterPatternLineEdit.textChanged.connect(lambda: self.filter_update(string_text=self.filterPatternLineEdit.text(),
model=self.proxyModel))
self.proxyView = QtGui.QTreeView()
self.proxyView.setColumnHidden(0, True)
self.proxyView.setRootIsDecorated(False)
self.proxyView.setAlternatingRowColors(True)
self.proxyView.setModel(self.proxyModel)
self.proxyView.setSortingEnabled(True)
self.proxyView.sortByColumn(1, QtCore.Qt.AscendingOrder)
proxyLayout = QtGui.QGridLayout()
proxyLayout.addWidget(self.proxyView, 0, 0, 1, 3)
proxyLayout.addWidget(self.filterPatternLineEdit, 1, 1)
proxyGroupBox = QtGui.QGroupBox("Sorted/Filtered Model")
proxyGroupBox.setLayout(proxyLayout)
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(proxyGroupBox)
self.setLayout(mainLayout)
self.setWindowTitle("Custom Sort/Filter Model")
self.resize(500, 450)
self.createMailModel()
self.setSourceModel(self.model)
def setSourceModel(self, model):
self.proxyModel.setSourceModel(model)
def filter_update(self, string_text=None, model=None):
syntax = QtCore.QRegExp.RegExp
caseSensitivity = QtCore.Qt.CaseInsensitive
regExp = QtCore.QRegExp(string_text, caseSensitivity, syntax)
model.setFilterRegExp(regExp)
def addMail(self, 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(self):
self.model.setHeaderData(0, QtCore.Qt.Horizontal, "Subject")
self.model.setHeaderData(1, QtCore.Qt.Horizontal, "Sender")
self.model.setHeaderData(2, QtCore.Qt.Horizontal, "Date")
self.addMail(self.model, "Happy New Year!", "Grace K. <grace@software-inc.com>",
QtCore.QDateTime(QtCore.QDate(2006, 12, 31), QtCore.QTime(17, 3)))
self.addMail(self.model, "Radically new concept", "Grace K. <grace@software-inc.com>",
QtCore.QDateTime(QtCore.QDate(2006, 12, 22), QtCore.QTime(9, 44)))
self.addMail(self.model, "Accounts", "pascale@nospam.com",
QtCore.QDateTime(QtCore.QDate(2006, 12, 31), QtCore.QTime(12, 50)))
self.addMail(self.model, "Expenses", "Joe Bloggs <joe@bloggs.com>",
QtCore.QDateTime(QtCore.QDate(2006, 12, 25), QtCore.QTime(11, 39)))
self.addMail(self.model, "Re: Expenses", "Andy <andy@nospam.com>",
QtCore.QDateTime(QtCore.QDate(2007, 1, 2), QtCore.QTime(16, 5)))
self.addMail(self.model, "Re: Accounts", "Joe Bloggs <joe@bloggs.com>",
QtCore.QDateTime(QtCore.QDate(2007, 1, 3), QtCore.QTime(14, 18)))
self.addMail(self.model, "Re: Accounts", "Andy <andy@nospam.com>",
QtCore.QDateTime(QtCore.QDate(2007, 1, 3), QtCore.QTime(14, 26)))
self.addMail(self.model, "Sports", "Linda Smith <linda.smith@nospam.com>",
QtCore.QDateTime(QtCore.QDate(2007, 1, 5), QtCore.QTime(11, 33)))
self.addMail(self.model, "AW: Sports", "Rolf Newschweinstein <rolfn@nospam.com>",
QtCore.QDateTime(QtCore.QDate(2007, 1, 5), QtCore.QTime(12, 0)))
self.addMail(self.model, "RE: Sports", "Petra Schmidt <petras@nospam.com>",
QtCore.QDateTime(QtCore.QDate(2007, 1, 5), QtCore.QTime(12, 1)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())