2015-05-29 66 views
1

插入多个QCheckBox到QTableWidget奇数行

我想创建一个160行的表,然后插入一个QCheckBox奇数行,特别是在第10列。问题是,我必须创建80 QCheckBox(每行一个,因此它们可以由用户分别分配)...

为我需要做的9个项目创建一个个80 QCheckBox对象就是废话!

有什么办法通过循环做到这一点?我想不出任何事情,我寻找答案,什么都没发现。

[...] 
# importing PySide 
from PySide import QtGui, QtCore 
[...] 
# Creating a Table 
class Table(QtGui.QDialog): 
    def __init__(self, parent=None): 
     super(Table, self).__init__(parent) 
     self.table = QtGui.QTableWidget() 
     self.table.setRowCount(160) 
     self.table.setColumnCount(10) 
# This is the tricky part: 
     chkBoxItem = QtGui.QTableWidgetItem() 
     chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled) 
     chkBoxItem.setCheckState(QtCore.Qt.Unchecked) 

     chkBoxItem2 = QtGui.QTableWidgetItem() 
     chkBoxItem2.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled) 
     chkBoxItem2.setCheckState(QtCore.Qt.Unchecked) 

     chkBoxItem3 = QtGui.QTableWidgetItem() 
     chkBoxItem3.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled) 
     chkBoxItem3.setCheckState(QtCore.Qt.Unchecked) 
[...] 
# Then insert all of them in the Table: 
     self.table.setItem(0, 10, chkBoxItem) 
     self.table.setItem(2, 10, chkBoxItem2) 
     self.table.setItem(4, 10, chkBoxItem3) 
     self.table.setItem(6, 10, chkBoxItem4) 
     self.table.setItem(8, 10, chkBoxItem5) 
     self.table.setItem(10, 10, chkBoxItem6) 
     self.table.setItem(12, 10, chkBoxItem7) 
[...] 

回答

1

该基本脚本创建含有160×10 QTable和QPushButton的UI。 每一个奇数行,复选框被添加到第10列的单元格中。 点击按钮显示所有复选框的状态列表。

  • 0:未选中
  • 2:经过
  • 有国家1,但我不记得是什么 它的用途,我会检查该文档。

注:

这已经使用PyQt的

取得

代码:

import math, sys 
from PyQt4.QtCore import Qt, QTimer 
from PyQt4.QtGui import * 

class MainWindow(QMainWindow): 
    def __init__(self, parent = None): 
     QMainWindow.__init__(self, parent) 

     #Create Basic UI 
     self.mainWidget = QWidget(self) 
     self.table = QTableWidget() 
     self.table.setRowCount(160) 
     self.table.setColumnCount(10) 

     self.button = QPushButton("Print stuff") 

     layout = QVBoxLayout(self.mainWidget) 
     layout.addWidget(self.table) 
     layout.addWidget(self.button) 

     self.setCentralWidget(self.mainWidget) 

     self.button.clicked.connect(self.printStuff) 
     ################# 

     #Fill the table 
     self.rowRange = range(0, self.table.rowCount(), 2) 
     for i in self.rowRange: 
      chkBoxItem = QTableWidgetItem() 
      chkBoxItem.setFlags(Qt.ItemIsUserCheckable|Qt.ItemIsEnabled) 
      chkBoxItem.setCheckState(Qt.Unchecked) 
      self.table.setItem(i, 9, chkBoxItem) 
     ############### 

    def printStuff(self): #You can remove this, this is for testing purpose only 
     print [(i+1, self.table.item(i, 9).checkState()) for i in self.rowRange] 

if __name__ == "__main__": 

    app = QApplication(sys.argv) 
    window = MainWindow() 
    window.show() 
    sys.exit(app.exec_()) 
+0

我有急事,现在做的,所以我的答案是不是很阐述。如果我可以休息一段时间,我会尽快回复你。你的问题也需要一点点版本,我会告诉你它有什么问题(没有什么重要的)。 – DrHaze

+0

非常感谢@DrHaze! –

+1

我已经改变了你原来的文章,你可以在这里看到编辑:[编辑](http://stackoverflow.com/posts/30531228/revisions) 我纠正了一些公式,删除了“Run snippet”,它主要是为js脚本。没有必要在你的帖子中道歉因为你的英文不好,这里的大多数人不是母语人士。只要你可以理解,没关系。无需说谢谢,或者感谢任何帮助,帮助SO上的人应该是一种乐趣(对我而言)。 SO的人很乐意帮助人们并在需要时获得帮助。欢迎来到SO btw。 – DrHaze