将多个 QCheckBox 插入到 QTableWidget 奇数行
Inserting multiple QCheckBox into QTableWidget odd rows
我正在尝试创建一个包含 160 行的 table,然后每隔奇数行插入一个 QCheckBox
,特别是在第 10 列。问题是,我必须创建 80 QCheckBox
(每行一个,因此它们可以由用户单独分配)...
逐个创建 80 QCheckBox
我要做的9个项目的objects简直是废话!
请问有什么循环的方法吗?什么都想不出来,找找答案,一无所获
[...]
# 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)
[...]
这个基本脚本创建一个 UI 包含一个 160*10 QTable 和一个 QPushButton。
每奇数行,在第 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_())
我正在尝试创建一个包含 160 行的 table,然后每隔奇数行插入一个 QCheckBox
,特别是在第 10 列。问题是,我必须创建 80 QCheckBox
(每行一个,因此它们可以由用户单独分配)...
逐个创建 80 QCheckBox
我要做的9个项目的objects简直是废话!
请问有什么循环的方法吗?什么都想不出来,找找答案,一无所获
[...]
# 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)
[...]
这个基本脚本创建一个 UI 包含一个 160*10 QTable 和一个 QPushButton。 每奇数行,在第 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_())