from PyQt4 import QtGui,QtCore
class Test(QtGui.QWidget):
def __init__(self,items,parent=None):
self.app=QtGui.QApplication([])
super(Test,self).__init__(parent)
self.setGeometry(200,100,200,300)
self.tw=QtGui.QTableWidget(len(items),1,self)
self.tw.setGeometry(0,0,200,300)
self.tw.setHorizontalHeaderLabels([‘Languages‘])
for i,item in enumerate(items):
cb=QtGui.QCheckBox(item)
cb.setCheckState(QtCore.Qt.Checked)
self.tw.setCellWidget(i,0,cb)
self.show()
self.app.exec_()
def closeEvent(self,event):
for i in range(self.tw.rowCount()):
cb=self.tw.cellWidget(i,0)
if cb.isChecked():
print ‘=>‘.join([str(cb.text()),‘Checked‘])
else:
print ‘=>‘.join([str(cb.text()),‘Unchecked‘])
if __name__==‘__main__‘:
Test([‘Python‘,‘Java‘,‘C++‘,‘C‘,‘JavaScript‘,‘C#‘])
sy【QTableWidget】【操作单元格控件】