-1

I have table 5x3 :first cloumn just temp text ,in second column contain combo box and last column is result by choice in combo box.
Now just set result into row 1 and i need set result to row for each combo box.(example:if change combo box in row 4 set item in row 4 etc..)

I found this example , but im not able implement to my script.

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()
Community
  • 1
  • 1
Ash
  • 35
  • 8

1 Answers1

2

Here is my solution:

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 is helpful, because you can pass extra parameters to the slot functions. Here is a useful link: http://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot

ProgrammingIsAwsome
  • 1,109
  • 7
  • 15