1

有表 10 行,2 列,在第一列中包含第二个临时文本中的复选框。
在按下名为 Copy 的按钮后,我需要检查第 1 列中的所有复选框(只需打印)。

..................................................... ………………………………………………………………………………………………………………………………………………

import PySide.QtCore as QtCore
import PySide.QtGui as QtGui


class chck( QtGui.QCheckBox ):
    def __init__( self, *args, **kwargs ):
        super( chck, self ).__init__( *args, **kwargs)

class TestTable( QtGui.QDialog ):
    def __init__( self, parent=None ):


        QtGui.QDialog.__init__( self, parent )
        self.resize( 300, 500 )
        self.myTable = QtGui.QTableWidget()
        self.myTable.setColumnCount( 2 )
        self.myTable.setRowCount( 10 )

        self.copyButton = QtGui.QPushButton(self.tr("Copy"))
        self.copyButton.clicked.connect(self.copy)



        for i in range( 0, self.myTable.rowCount() ):
            ok = chck( '' +str(i+1))
            self.myTable.setCellWidget( i,0,ok)  # set check box                   
            item = QtGui.QTableWidgetItem("text" + str(i+1))# set tmp text 
            self.myTable.setItem(i,1,item)


        buttonLayout = QtGui.QHBoxLayout()
        buttonLayout.addStretch(1)
        buttonLayout.addWidget(self.copyButton)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.myTable)   
        layout.addLayout(buttonLayout) 

        self.setLayout(layout)




    def copy( self ):
        for i in range( 0, self.myTable.rowCount() ):
            print "Check box " + str(i+1) + " is :" # print status QCheckBox if is on or off



tableView = TestTable()
tableView.show()
4

1 回答 1

0

Get the checkbox with cellWidget, and then use isChecked:

def copy(self):
    for i in range(0, self.myTable.rowCount()):
        checkbox = self.myTable.cellWidget(i, 0)
        print "Check box " + str(i+1) + " is :",
        print 'checked' if checkbox.isChecked() else 'not checked'
于 2014-12-16T17:52:42.083 回答