I have a table with multiple rows and on column 0 I put it a check-box, defined like this:
for char in accounts:
for columnNumber in range(numberColumns):
pWidget = QWidget()
pCheckbox = QCheckBox()
pLayout = QVBoxLayout(pWidget)
pLayout.addWidget(pCheckbox)
pLayout.setAlignment(Qt.AlignCenter)
pLayout.setContentsMargins(0, 0 ,0, 0)
pCheckbox.setCheckState(False)
pCheckbox.clicked.connect(self.handleItemClicked)
pWidget.setLayout(pLayout)
self.mainAccountTable.insertRow(currentRowCount)
self.mainAccountTable.setCellWidget(currentRowCount, 0, pWidget)
self.mainAccountTable.setItem(currentRowCount, 1, QTableWidgetItem(char[1]))
And I have a method connected for handling the clicks:
def handleItemClicked(self):
try:
#self.accountsSelected = []
for account in range(self.mainAccountTable.rowCount()):
if self.mainAccountTable.cellWidget(account, 0).findChild(type(QCheckBox())).isChecked():
self.accountsSelected.add(self.mainAccountTable.item(account, 1).text())
print ("yes:",self.accountsSelected)
else:
self.accountsSelected.remove(self.mainAccountTable.item(account, 1).text())
print ("no:",self.accountsSelected)
except Exception as e:
print ("Error",e)
What bothers me is that is working perfect, but it's refreshing the results only if I check or uncheck the the first check-box (row 0, column 0). I tried also with connecting the signal with toggled
... same result. So, how can I make it to update the results when I check or uncheck others rows except the first one? Thanks in advance.
Later Edit: I update it my code and the output is like this: If I check other checkboxes except the first one this is the output: 'Account1'
and If I have selected, let's say 5 accounts and I have the first checkbox also selected this is the output:
yes: {'Account2', 'Account1'}
yes: {'Account2', 'Account1'}
yes: {'Account2', 'Account1', 'Account3'}
yes: {'Account2', 'Account1', 'Account3', 'Account4'}
yes: {'Account2', 'Account1', 'Account3', 'Account4', 'Account5'}
Error: 'Account 6' #is not selected which is true, BUT WHY is checking also for that?!?!
there are n = rowCount()
pCheckBoxes. but only the clicked()
-signal of 1 pCheckBox is connected to self.handleItemClicked()
. Take the connect()
in the first part of code, to connect every pCheckBox. As there are n pCheckboxes, pWidgets, pLayouts delete the 'self':
pWidget = QWidget()
pCheckbox = QCheckBox()
pLayout = QVBoxLayout(pWidget)
pLayout.addWidget(pCheckbox)
pLayout.setAlignment(Qt.AlignCenter)
pLayout.setContentsMargins(0, 0 ,0, 0)
pCheckbox.setCheckState(False)
pCheckBox.clicked.connect(self.handleItemClicked)
pWidget.setLayout(pLayout)
self.mainAccountTable.insertRow(currentRowCount)
self.mainAccountTable.setCellWidget(currentRowCount, 0, pWidget)
The bug was in "else" branch and the work around is to have 2 for loops for each check:
def handleItemClicked(self, account):
try:
accountsSelected = set()
for account in range(self.mainAccountTable.rowCount()):
if self.mainAccountTable.cellWidget(account, 0).findChild(type(QCheckBox())).isChecked():
accountsSelected.add(self.mainAccountTable.item(account, 1).text())
print ("selected:",accountsSelected)
for account in range(self.mainAccountTable.rowCount()):
if not self.mainAccountTable.cellWidget(account, 0).findChild(type(QCheckBox())).isChecked():
accountsSelected.remove(self.mainAccountTable.item(account, 1).text())
print ("not selected:",accountsSelected)
print ("main:", accountsSelected)
return accountsSelected
except Exception as e:
print ("error:",e)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.