单个表格 cell 内绑定若干个 button
单独的函数用于往 table 每一行指定 Action 列的单个 cell 中写入若干个 button
def fn_add_button_for_row(list_info, row_index, curr_column_name):
dict_button = dict()
widget = QWidget()
hLayout = QHBoxLayout()
# add several buttons
for index, value in enumerate(list_info[row_index][curr_column_name]):
btn = QtWidgets.QPushButton(value)
btn.setStyleSheet(''' text-align : center;
background-color : LightCoral;
height : 30px;
border-style: outset;
font : 13px; ''')
dict_button['btn' + str(index + 1)] = btn
hLayout.addWidget(btn)
hLayout.setContentsMargins(5,2,5,2)
widget.setLayout(hLayout)
return dict_button, widget
显示整个 table 时须调用上述函数:
for row_index in range(row_num):
dict_button = dict()
item = QtWidgets.QTableWidgetItem()
certain_table.setVerticalHeaderItem(row_index, item)
certain_table.verticalHeaderItem(row_index).setText(_translate("MainWindow", str(row_index + 1))) # set table vertical header index
for column_index in range(column_num):
item = QtWidgets.QTableWidgetItem()
item.setFlags(QtCore.Qt.ItemIsEnabled)
certain_table.setItem(row_index, column_index, item)
curr_column_name = list_column_name[column_index]
value = list_info[row_index][curr_column_name]
if curr_column_name != "Action": # get the letters except the last two digits
certain_table.item(row_index, column_index).setText(_translate("Dialog", str(value)))
else:
# draw buttons for actions
dict_button, widget = fn_add_button_for_row(list_info, row_index, curr_column_name)
certain_table.setCellWidget(row_index, column_index, widget)
网友评论