这是我的代码,我想用一个键将项目列表添加到qtabl小部件中,当我要将项目列表添加到qtable小部件中时我有一个项目列表,我遇到了问题,即我的最后一个项目由于列中的idx索引而覆盖了上一个项目。我尝试了多种方法,但无法对qtable小部件中的所有项目进行操作。
下面是我的示例尝试代码:
from PyQt4 import QtCore, QtGui
from functools import partial
import os, sys
import time
from datetime import datetime
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
self.data_arraylist = {'85207':[{'item1':"Fruites", 'Qty':'10','Price':'100','total':'100'},{'item1':"Banana", 'Qty':'20','Price':'200','total':'200'}],'97895':{'item1':"vegitables", 'Qty':'2kg','Price':'200','total':'200'},'97055':{'item1':"snacks", 'Qty':'10p','Price':'200','total':'200'}}
super(MainWindow, self).__init__(parent)
self.dataw = QtGui.QWidget()
self._gridlayout = QtGui.QGridLayout()
self.vboxdata = QtGui.QVBoxLayout(self.dataw)
self.scrollArea = QtGui.QScrollArea()
self.vboxdata.addWidget(self.scrollArea)
self.scrollArea.setWidgetResizable(True)
self.vbox = QtGui.QVBoxLayout()
self.hbox1 = QtGui.QHBoxLayout()
self.timelabel = QtGui.QLabel()
self.orderbtn = QtGui.QPushButton("Orders")
self.newbtn = QtGui.QPushButton("New")
self.newbtn.clicked.connect(self.items_list)
self.hbox1.addWidget(self.timelabel)
self.hbox1.addWidget(self.orderbtn)
self.hbox1.addWidget(self.newbtn)
self.vbox.addLayout(self.hbox1)
self.mainLayout = QtGui.QGridLayout()
self.mainLayout.addWidget(self.dataw, 0, 0)
self.mainLayout.addLayout(self.vbox, 1, 0)
self.setCentralWidget(QtGui.QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
def items_list(self):
self.mainw2 = QtGui.QWidget()
self.scrollArea.setWidget(self.mainw2)
self.newvbox = QtGui.QVBoxLayout(self.mainw2)
self.linedit = QtGui.QLineEdit()
self.search = QtGui.QLabel("Search")
self.newhbox = QtGui.QHBoxLayout()
self.newhbox.addWidget(self.linedit)
self.newhbox.addWidget(self.search)
self.newvbox.addLayout(self.newhbox)
self.table = QtGui.QTableWidget()
self.table_item = QtGui.QTableWidgetItem()
self.table.setRowCount(5)
self.table.verticalHeader().hide()
self.table.setColumnCount(5)
self.table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.table.setHorizontalHeaderLabels(("S.no, Item Description,Qty,Rate(Rs:),Total").split(','))
self.newvbox.addWidget(self.table)
def keyPressEvent(self, event):
print event,event.key(),QtCore.Qt.Key_Return
if event.key() == QtCore.Qt.Key_Return:
text_key = self.linedit.text()
self.searchitems(text_key)
def searchitems(self,text_key):
if str(text_key) in self.data_arraylist:
for ridx,row in enumerate(range(1)):
for idx, column in enumerate(range(5)):
if idx ==0:
self.table.setItem(row,column,QtGui.QTableWidgetItem(str(ridx+1)))
elif idx ==1:
for i in range(len(self.data_arraylist[str(text_key)])):
self.table.setItem(row,column,QtGui.QTableWidgetItem(
str(self.data_arraylist[str(text_key)][i][
'item1'])))
elif idx ==2:
for i in range(len(self.data_arraylist[str(text_key)])):
self.table.setItem(row,column,
QtGui.QTableWidgetItem(str(
self.data_arraylist[str(
text_key)][i]['Qty'])))
elif idx ==3:
for i in range(len(self.data_arraylist[str(text_key)])):
self.table.setItem(row,column,QtGui.QTableWidgetItem(str(self.data_arraylist[str(text_key)][i]['Price'])))
elif idx ==4:
for i in range(len(self.data_arraylist[str(text_key)])):
self.table.setItem(row,column,QtGui.QTableWidgetItem(str(self.data_arraylist[str(text_key)][i]['total'])))
else:
pass
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
w.setGeometry(500,500,800,400)
sys.exit(app.exec_())