2014-04-10 51 views
1

我正在使用QMainWindow並添加一個QTableView小部件。該表將填充來自csv文件的數據。 csv文件的第一行有標題,但我找不到如何將這一行寫入標題。即使輸入測試標題列表也不起作用。pyqt4在QMainWindow中使用csv輸入和標題的QTableView

另外我想對「時間」列進行反向排序。

這裏是代碼限制到大都表:

import sys 
import csv 
from PyQt4 import QtGui 
from PyQt4.QtCore import * 
from array import * 


class UserWindow(QtGui.QMainWindow): 

    def __init__(self, parent=None): 
     super(UserWindow, self).__init__() 
     self.specModel = QtGui.QStandardItemModel(self) 
     self.specList = self.createSpecTable() 
     self.initUI() 

    def specData(self): 
     with open('testFile.csv', 'rb') as csvInput: 
      for row in csv.reader(csvInput): 
       if row > 0: 
        items = [QtGui.QStandardItem(field) for field in row] 
        self.specModel.appendRow(items) 

    def createSpecTable(self): 
     self.specTable = QtGui.QTableView() 
     # This is a test header - different from what is needed 
     specHdr = ['Test', 'Date', 'Time', 'Type'] 
     self.specData() 
     specM = specTableModel(self.specModel, specHdr, self) 
     self.specTable.setModel(specM) 
     self.specTable.setShowGrid(False) 
     vHead = self.specTable.verticalHeader() 
     vHead.setVisible(False) 
     hHead = self.specTable.horizontalHeader() 
     hHead.setStretchLastSection(True) 
     self.specTable.sortByColumn(3, Qt.DescendingOrder) 
     return self.specTable 

    def initUI(self): 
     self.ctr_frame = QtGui.QWidget() 

     self.scnBtn = QtGui.QPushButton("Sample") 
     self.refBtn = QtGui.QPushButton("Reference") 
     self.stpBtn = QtGui.QPushButton("Blah") 

# List Window 
     self.specList.setModel(self.specModel) 

# Layout of Widgets 
     pGrid = QtGui.QGridLayout() 
     pGrid.setSpacing(5) 
     pGrid.addWidget(self.scnBtn, 3, 0, 1, 2) 
     pGrid.addWidget(self.refBtn, 3, 2, 1, 2) 
     pGrid.addWidget(self.stpBtn, 3, 4, 1, 2) 
     pGrid.addWidget(self.specList, 10, 0, 20, 6) 
     self.ctr_frame.setLayout(pGrid) 
     self.setCentralWidget(self.ctr_frame) 
     self.statusBar() 

     self.setGeometry(300, 300, 400, 300) 
     self.setWindowTitle('Test') 


class specTableModel(QAbstractTableModel): 
    def __init__(self, datain, headerdata, parent=None, *args): 

     QAbstractTableModel.__init__(self, parent, *args) 
     self.arraydata = datain 
     self.headerdata = headerdata 

    def rowCount(self, parent): 
     return len(self.arraydata) 

    def columnCount(self, parent): 
     return len(self.arraydata[0]) 

    def data(self, index, role): 
     if not index.isValid(): 
      return QVariant() 
     elif role != Qt.DisplayRole: 
      return QVariant() 
     return QVariant(self.arraydata[index.row()][index.column()]) 

    def headerData(self, col, orientation, role): 
     if orientation == Qt.Horizontal and role == Qt.DisplayRole: 
      return self.headerdata[col] 
     return None 


def main(): 

    app = QtGui.QApplication(sys.argv) 
    app.setStyle(QtGui.QStyleFactory.create("plastique")) 
    ex = UserWindow() 
    ex.show() 
    sys.exit(app.exec_()) 

if __name__ == '__main__': 
    main() 

,這裏是一個非常短的CSV文件:

Run,Date,Time,Comment 
data1,03/03/2014,00:04,Reference 
data2,03/03/2014,02:00,Reference 
data5,03/03/2014,02:08,Sample 
data6,03/03/2014,13:57,Sample 

而且根據rowCount &定義信息columnCount不工作。

回答

1

解決了我發佈的問題:寫了一個'getHeader'函數,只是爲了讀取csv文件的第一行並返回列表。將以下內容添加到createSpecTable函數中:

specHdr = self.getHeader() 
self.specModel.setHorizontalHeaderLabels(specHdr) 
self.specModel.sort(2, Qt.DescendingOrder) 

最後一條語句解決了反向排序問題。從CSV文件中的標題行中加入最後一行到specData函數從表中移除:

def rowCount(self, parent): 
    return self.arraydata.rowCount() 

def columnCount(self, parent): 
    return self.arraydata.columnCount() 

self.specModelremoveRow(0). 

最後根據rowCount和信息columnCount用校正