gpt4 book ai didi

python - 如何从 PyQt5 中动态生成的复选框中删除标签?

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:39 25 4
gpt4 key购买 nike

所以我有一个表,其中有几个使用这些代码行动态生成的复选框:

chkBoxItem = QTableWidgetItem()
chkBoxItem.setTextAlignment(Qt.AlignHCenter)
chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
ui.columnTable.cellChanged.connect(self.checkColumnDataCheckbox)
ui.columnTable.setItem(i , 0, chkBoxItem)

我尝试将其大小调整为一个较小的值,但它不起作用:

chkBoxItem.setSizeHint(QSize(10,10))

似乎没有超出某个最小宽度和高度。
它看起来是这样的: enter image description here

基本上,我想删除此文本标签,以便复选框可以位于单元格的中心

最佳答案

void QTableWidget::setCellWidget(int row, int column, QWidget *widget)

Sets the given widget to be displayed in the cell in the given row and column, passing the ownership of the widget to the table.

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window(QWidget):
def __init__(self, rows, columns):
super().__init__()

self.table = QTableWidget(rows, columns, self)

for row in range(rows):
widget = QWidget()
checkbox = QCheckBox()
checkbox.setCheckState(Qt.Unchecked)
layoutH = QHBoxLayout(widget)
layoutH.addWidget(checkbox)
layoutH.setAlignment(Qt.AlignCenter)
layoutH.setContentsMargins(0, 0, 0, 0)

self.table.setCellWidget(row, 0, widget) # <----
self.table.setItem(row, 1, QTableWidgetItem(str(row)))

self.button = QPushButton("Control selected QCheckBox")
self.button.clicked.connect(self.ButtonClicked)

layoutV = QVBoxLayout(self)
layoutV.addWidget(self.table)
layoutV.addWidget(self.button)


def ButtonClicked(self):
checked_list = []
for i in range(self.table.rowCount()):
if self.table.cellWidget(i, 0).findChild(type(QCheckBox())).isChecked():
checked_list.append(self.table.item(i, 1).text())
print(checked_list)


if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window(3, 3)
window.resize(350, 300)
window.show()
sys.exit(app.exec_())

enter image description here

关于python - 如何从 PyQt5 中动态生成的复选框中删除标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56102229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com