Pyqt5的tableWidget的单元格控件居中
以在tableWidget添加checkBox为例。
#实例化一个tableWidget1
tableWidget1 = QtWidgets.QTableWidget()
tableWidget1.setRowCount(6)
tableWidget1.setColumnCount(3)
tableWidget1.setHorizontalHeaderLabels(['图名','开始刻度','结束刻度'])
tableWidget1.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
tableWidget1.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
#以列表存储CheckBox的名称
items = ["B1-Dir X","B1-Dir Y","B2-Dir X","B2-Dir Y","B3-Dir X","B3-Dir Y"]
for i in range(len(items)):
checkbox1 = QtWidgets.QCheckBox()
checkbox1.setText(items[i])
#正式开始控件居中:
#1.实例化一个新布局
hLayout = QtWidgets.QHBoxLayout()
#2.在布局里添加checkBox
hLayout.addWidget(checkbox1)
#3.在布局里居中放置checkbox1