2013-01-04 37 views
0

我從下面的代碼片段獲得了stackoverflow追隨者的幫助。如何在pyqt中爲Qtables創建組合過濾器

我現在可以過濾表格。但是當我嘗試過濾它首先排序,因爲我啓用排序視圖。

我想創建QTableview這樣的一種方式,如果我點擊頭應該排序。並且在每個標題的右側應該有一個下拉框(可以是combox box style)。我正在上傳的我多麼希望瞬間(這是我在.NET使它)

代碼段

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 

from PyQt4 import QtCore, QtGui 

class myWindow(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     super(myWindow, self).__init__(parent) 
     self.centralwidget = QtGui.QWidget(self) 
     self.view   = QtGui.QTableView(self.centralwidget) 
     self.view.setSortingEnabled(True) 
     self.gridLayout = QtGui.QGridLayout(self.centralwidget) 
     self.gridLayout.addWidget(self.view, 1, 0, 1, 3) 

     self.setCentralWidget(self.centralwidget) 

     self.model = QtGui.QStandardItemModel(self) 

     for rowName in range(3) * 5: 
      self.model.invisibleRootItem().appendRow(
       [ QtGui.QStandardItem("row {0} col {1}".format(rowName, column))  
        for column in range(3) 
        ] 
       ) 
     self.proxy = QtGui.QSortFilterProxyModel(self) 
     self.proxy.setSourceModel(self.model) 

     self.view.setModel(self.proxy) 

     self.horizontalHeader = self.view.horizontalHeader() 
     self.horizontalHeader.sectionClicked.connect(self.horizontalHeader_Clicked) 

    @QtCore.pyqtSlot(int) 
    def horizontalHeader_Clicked(self, logicalIndex): 
     self.logicalIndex = logicalIndex 
     # local variable, and no parent 
     menuValues = QtGui.QMenu() 
     # delete the previous one 
     try: 
      self.signalMapper.deleteLater() 
     except: 
      pass 

     self.signalMapper = QtCore.QSignalMapper(self) 

     valuesUnique = [  
      self.proxy.index(row, self.logicalIndex).data().toString() 
      for row in xrange(self.proxy.rowCount()) 
      ] 

     print 'printing col %d values' % self.logicalIndex        
     for row in range(self.proxy.rowCount()): 
      print 'row %d Item %s' % (row,self.model.item(row,  self.logicalIndex).text()) 


     actionAll = QtGui.QAction("All", self) 
     actionAll.triggered.connect(self.actionAll) 
     menuValues.addAction(actionAll) 
     menuValues.addSeparator() 

     for actionNumber, actionName in enumerate(sorted(list(set(valuesUnique)))):    

      action = QtGui.QAction(actionName, self) 
      self.signalMapper.setMapping(action, actionNumber) 
      action.triggered.connect(self.signalMapper.map) 
      menuValues.addAction(action) 

     self.signalMapper.mapped.connect(self.signalMapper_mapped) 

     headerPos = self.view.mapToGlobal(self.horizontalHeader.pos())   

     posY = headerPos.y() + self.horizontalHeader.height() 
     posX = headerPos.x() + self.horizontalHeader.sectionPosition(self.logicalIndex) 

     menuValues.exec_(QtCore.QPoint(posX, posY)) 

    @QtCore.pyqtSlot() 
    def actionAll(self): 
     filterColumn = self.logicalIndex 
     filterString = QtCore.QRegExp( "", 
            QtCore.Qt.CaseInsensitive, 
            QtCore.QRegExp.RegExp 
            ) 

     self.proxy.setFilterRegExp(filterString) 
     self.proxy.setFilterKeyColumn(filterColumn) 

    @QtCore.pyqtSlot(int) 
    def signalMapper_mapped(self, i): 
     stringAction = self.signalMapper.mapping(i).text() 
     filterColumn = self.logicalIndex 
     filterString = QtCore.QRegExp( stringAction, 
            QtCore.Qt.CaseSensitive, 
            QtCore.QRegExp.FixedString 
            ) 

     self.proxy.setFilterRegExp(filterString) 
     self.proxy.setFilterKeyColumn(filterColumn) 

if __name__ == "__main__": 
    import sys 

    app = QtGui.QApplication(sys.argv) 
    main = myWindow() 
    main.show() 
    main.resize(400, 600) 
    sys.exit(app.exec_()) 

這我如何我試圖讓(排序和篩選)

enter image description here

如果可能的話,我需要能夠像上圖中那樣爲選定的列設置過濾器。

回答

1

這裏有一個討論的位置大致相同的話題:Quick way for QWidget in QHeaderView's columns?

他們認爲你需要拋棄股票QHeaderView在您看來,併爲標題的功能自己的自定義窗口小部件,以地方自定義小部件放入標題部分。