我试图在QTableWidget的某个单元格中插入一些QRadioButton。情况与this post类似。具体来说,@eyllansc与PySide2
的解决方案如下
import sys
from PySide2.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, \
QButtonGroup, QRadioButton
app = QApplication(sys.argv)
searchView = QTableWidget(0, 4)
colsNames = ['A', 'B', 'C']
searchView.setHorizontalHeaderLabels(['DIR'] + colsNames)
dirNames = {'A': ['/tmp', '/tmp/dir1'], 'B': ['/tmp/dir2'],
'C': ['/tmp/dir3']}
rowCount = sum(len(v) for (name, v) in dirNames.items())
searchView.setRowCount(rowCount)
index = 0
for letter, paths in dirNames.items():
for path in paths:
it = QTableWidgetItem(path)
searchView.setItem(index, 0, it)
group = QButtonGroup(searchView)
for i, name in enumerate(colsNames):
button = QRadioButton()
group.addButton(button)
searchView.setCellWidget(index, i + 1, button)
if name == letter:
button.setChecked(True)
index += 1
searchView.show()
sys.exit(app.exec_())
在调整列或行的大小时,我注意到一种奇怪的行为:当我按下鼠标按钮并调整列或原始列的大小时,QRadioButtons仍然保持在它们的位置,导致一些冲突;然后,当我最终释放鼠标按钮时,每个QRadioButton都会回到它的位置。有没有办法避免aka在调整大小的过程中使QRadioButtons也移动
由于我以前的解决方案会产生其他问题,因此在此解决方案中,我将展示另一种备选方案:
相关问题 更多 >
编程相关推荐