我有一个带有QComboBox小部件的表格视图。所有QComboBox都具有相同的初始值,但是一旦选择了一个值,就必须将其从表的其余QComboBox中删除。有可能吗?
我有一个信号,当QComboBox中的文本更改时,它会保存在一组使用的字母中。因此,我需要从表格视图的其余QComboBox中删除该字母。
我的下面的代码:
from PyQt5.QtWidgets import QWidget, QComboBox
from PyQt5.QtGui import QStandardItemModel, QStandardItem
class widgetTabContent(QWidget, Ui_widgetTabContent):
def __init__(self, parent=None):
super(widgetTabContent, self).__init__(parent)
self.setupUi(self)
self.QSModel = QStandardItemModel()
self.tvColumns.setModel(self.QSModel)
self.letters = []
self.cb_letters_used = set()
self.fill_table()
def combobox_current_text_changed(self, text):
print(text)
if text:
self.cb_letters_used.add(text)
print(self.cb_letters_used)
# Commented this code as it becomes logically recursive
# self.cb_update_list = [letter for letter in self.letters
# if letter not in self.cb_letters_used]
# for row in range(self.QSModel.rowCount()):
# self.tvColumns.indexWidget(self.tvColumns.model().index(row, 0)).clear()
# self.tvColumns.indexWidget(self.tvColumns.model().index(row, 0)).addItems(self.cb_update_list)
def fill_table(self):
self.letters = ['A','B','C','D','E','F','G']
self.QSModel.setColumnCount(1)
for row in range(len(self.col_letters)):
combobox = QComboBox()
combobox.add_items(self.col_letters)
combobox.currentTextChanged.connect(self.combobox_current_text_changed)
self.QSModel.insertRow(row)
self.tvColumns.setIndexWidget(self.tvColumns.model().index(row, 0), combobox)