How To Get Selected Items From Qcombobox To Be Displayed In Qtablewidget In Pyqt5? (qcombobox Has Checkbox To Pick Items)
I have QTableWidget in which I have QComboBox in each row for particular column. My each QCombobox has multiple values with checkboxes. I want to display selected item/s from each
Solution 1:
You should find a way to keep track of the row for each combo model, so that you can set the item text accordingly.
Note that I changed your code logic a bit, as there were some conceptual mistakes:
- don't set instance attributes if they are not required: all those
self.rowPosition
,self.monthList
, etc, change everytime the for loop cycles, and you don't need them after that; - avoid using uppercase names for variables and functions
- the month list is always the same, build it before the for cycle instead of computing it each time
- instead of setting the model to the combo in the function, make it return the model
Edit: added copy function after comment request
defcreateModel(options):
model = QStandardItemModel(len(options), 1)
firstItem = QtGui.QStandardItem("SelectMonths")
firstItem.setBackground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))
firstItem.setSelectable(False)
model.setItem(0, 0, firstItem)
for i, area inenumerate(options):
item = QStandardItem(area)
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setData(Qt.Unchecked, Qt.CheckStateRole)
model.setItem(i+1, 0, item)
return model
classDemoCode(QtWidgets.QMainWindow, Ui_MainWindow):
def__init__(self):
super(DemoCode, self).__init__()
self.setupUi(self)
monthList = [(date.today() + relativedelta(months=-n)).strftime('1-%b-%Y') for n inrange(1, 5)]
self.models = {}
rows = self.tableWidget.rowCount()
for row inrange(rows, rows + 3):
self.tableWidget.insertRow(row)
comboBox = QtWidgets.QComboBox()
model = createModel(monthList)
comboBox.setModel(model)
model.itemChanged.connect(lambda _, row=row: self.on_itemChanged(row))
self.models[row] = model
self.tableWidget.setCellWidget(row, 0, comboBox)
item = QTableWidgetItem()
self.tableWidget.setItem(row, 1, item)
self.tableWidget.setColumnWidth(1,150)
defon_itemChanged(self, tableRow):
model = self.models[tableRow]
items = []
for row inrange(model.rowCount()):
comboItem = model.index(row, 0)
if comboItem.data(Qt.CheckStateRole):
items.append(comboItem.data())
self.tableWidget.item(tableRow, 1).setText(', '.join(items))
defcopyFromRow(self, tableRow):
sourceModel = self.models[tableRow]
checkedRows = []
for row inrange(sourceModel.rowCount()):
if sourceModel.index(row, 0).data(Qt.CheckStateRole):
checkedRows.append(row)
for model in self.models.values():
if model == sourceModel:
continuefor row inrange(model.rowCount()):
model.setData(
model.index(row, 0),
Qt.Checked if row in checkedRows else Qt.Unchecked,
Qt.CheckStateRole)
I'd also suggest to remove the "Select Months" and "Selected Months" items, and use them as table headers.
Post a Comment for "How To Get Selected Items From Qcombobox To Be Displayed In Qtablewidget In Pyqt5? (qcombobox Has Checkbox To Pick Items)"