我正在使用QTableWidget来显示csv数据。当我输入关键字时,我希望执行类似于excel的过滤操作。它应该影响所有的行和列。
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import QString
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import MySQLdb
import os
import time
import sys
import hashlib
import getpass
import webbrowser
import csv
filter_class = uic.loadUiType("filter.ui")[0]
class Filter_window(QtGui.QWidget, filter_class):
def __init__(self, parent=None, *args, **kwargs):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
#self.selectall.clicked.connect(self.selectall_application)
#self.filnext.clicked.connect(self.filnext_application)
self.loadAll()
def loadAll(self):
with open("Rts.csv", "rb") as inpfil:
reader = csv.reader(inpfil, delimiter = ',')
csheader = reader.next()
ncol = len(csheader)
data = list(reader)
row_count = len(data)
self.filterall.setRowCount(row_count)
self.filterall.setColumnCount(ncol)
self.filterall.setHorizontalHeaderLabels(QString('%s' % ', '.join(map(str, csheader))).split(","))
for ii in range(0, row_count):
print data[ii]
mainins = data[ii]
print mainins
for var in range(0,ncol):
print mainins[var], "\n"
self.filterall.setItem(ii, var, QTableWidgetItem(mainins[var]))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
filterwin = Filter_window()
filterwin.show()
sys.exit(app.exec_())
filterall是对象名称为QTableWidget
.ui代码:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>allCases</class>
<widget class="QWidget" name="allCases">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>597</width>
<height>466</height>
</rect>
</property>
<property name="windowTitle">
<string>All Cases</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="0" colspan="4">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="4" column="3">
<widget class="QPushButton" name="filnext">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>Next</string>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QPushButton" name="selectall">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>Select All</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="filterEdit">
<property name="toolTip">
<string/>
</property>
<property name="statusTip">
<string/>
</property>
<property name="placeholderText">
<string>Enter keyword to search</string>
</property>
</widget>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0" colspan="4">
<widget class="QTableWidget" name="filterall"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>