我有表5x3:第一个cloumn只是临时文本,第二列包含组合框,最后一列是组合框中的选择结果。
现在只需将结果设置为第1行,我需要将结果设置为每个组合框的行。(例如:如果更改第4行中第4行设置项的组合框等等。)
我发现了这个example,但我无法实现我的脚本。
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
class cBox( QtGui.QComboBox ):
def __init__( self, *args, **kwargs ):
super( cBox, self ).__init__( *args, **kwargs)
class TestTable( QtGui.QWidget ):
def __init__( self, parent=None ):
QtGui.QWidget.__init__( self, parent )
self.setLayout( QtGui.QVBoxLayout() )
self.resize( 600, 300 )
self.myTable = QtGui.QTableWidget()
self.myTable.setColumnCount( 3 )
self.myTable.setRowCount( 5 )
self.setTable()
self.layout().addWidget(self.myTable)
self.myTable.cellChanged.connect(self.update)
def setTable(self):
for i in range( 0, self.myTable.rowCount() ):
item = "row " + str(i)
self.myTable.setItem(i, 0, QtGui.QTableWidgetItem(item))
box = cBox()
nameList = ("sss","aaa","qqq")
box.addItems(nameList)
self.myTable.setCellWidget( i,1,box)
box.currentIndexChanged.connect(self.tmp)
def tmp(self,i):
row = 1 # this I need change for actual row ,now just set to row 1
item = "item " + str(i)
self.myTable.setItem(row, 2, QtGui.QTableWidgetItem(item)) #set item to 3 column by selected item form combo box
if __name__ == "__main__":
tableView = TestTable()
tableView.show()
答案 0 :(得分:2)
这是我的解决方案:
from functools import partial
def setTable(self):
for i in range( 0, self.myTable.rowCount() ):
item = "row " + str(i)
self.myTable.setItem(i, 0, QtGui.QTableWidgetItem(item))
box = cBox()
nameList = ("sss","aaa","qqq")
box.addItems(nameList)
self.myTable.setCellWidget(i,1,box)
# here the 'i' will be the parameter rowIndex in the tmp function
box.currentIndexChanged.connect(partial(self.tmp, i))
def tmp(self, rowIndex, comboBoxIndex):
# this print statements are just for explanation
print "current index of the combobox: " + str(comboBoxIndex)
print "row index of where the combobox resides: " + str(rowIndex)
item = "item " + str(comboBoxIndex)
self.myTable.setItem(rowIndex, 2, QtGui.QTableWidgetItem(item))
partial是有用的,因为您可以将额外的参数传递给插槽函数。 这是一个有用的链接:http://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot