单击项目的 PyQt 奇怪行为
PyQt strange behavior on clicked items
我有一个包含多行的 table,在第 0 列我放了一个复选框,定义如下:
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]))
我有一个处理点击的连接方法:
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)
令我困扰的是它运行完美,但只有当我选中或取消选中第一个复选框(第 0 行,第 0 列)时它才会刷新结果。我也尝试将信号与 toggled
连接......结果相同。
那么,当我选中或取消选中除第一行以外的其他行时,如何更新结果呢?提前致谢。
稍后编辑:
我更新了我的代码,输出是这样的:
如果我选中除第一个复选框以外的其他复选框,这就是输出:
'Account1'
如果我选择了,假设有 5 个帐户,并且我还选中了第一个复选框,这是输出:
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?!?!
有 n = rowCount()
个复选框。但只有 1 个 pCheckBox 的 clicked()
信号连接到 self.handleItemClicked()
。拿代码第一部分的connect()
,连接每一个pCheckBox。由于有 n 个 pCheckboxes、pWidgets、pLayouts 删除 '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)
错误出现在 "else" 分支中,解决方法是为每次检查设置 2 个 for 循环:
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)
我有一个包含多行的 table,在第 0 列我放了一个复选框,定义如下:
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]))
我有一个处理点击的连接方法:
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)
令我困扰的是它运行完美,但只有当我选中或取消选中第一个复选框(第 0 行,第 0 列)时它才会刷新结果。我也尝试将信号与 toggled
连接......结果相同。
那么,当我选中或取消选中除第一行以外的其他行时,如何更新结果呢?提前致谢。
稍后编辑:
我更新了我的代码,输出是这样的:
如果我选中除第一个复选框以外的其他复选框,这就是输出:
'Account1'
如果我选择了,假设有 5 个帐户,并且我还选中了第一个复选框,这是输出:
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?!?!
有 n = rowCount()
个复选框。但只有 1 个 pCheckBox 的 clicked()
信号连接到 self.handleItemClicked()
。拿代码第一部分的connect()
,连接每一个pCheckBox。由于有 n 个 pCheckboxes、pWidgets、pLayouts 删除 '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)
错误出现在 "else" 分支中,解决方法是为每次检查设置 2 个 for 循环:
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)