简体   繁体   English

PyQt对单击项的奇怪行为

[英]PyQt strange behavior on clicked items

I have a table with multiple rows and on column 0 I put it a check-box, defined like this: 我有一个包含多行的表,在第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]))

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). 令我困扰的是,它工作得很好,但是只有当我选中或取消选中第一个复选框(第0行,第0列)时,它才会刷新结果。 I tried also with connecting the signal with toggled ... same result. 我也尝试过将信号与已toggled ...连接相同。 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: 以后的编辑:我更新它的代码,输出是这样的:如果我选中除第一个复选框以外的其他复选框,则输出为: '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?!?!

there are n = rowCount() pCheckBoxes. 有n = rowCount() pCheckBoxes。 but only the clicked() -signal of 1 pCheckBox is connected to self.handleItemClicked() . 但只有1个pCheckBox的clicked()信号连接到self.handleItemClicked() Take the connect() in the first part of code, to connect every pCheckBox. 在代码的第一部分中使用connect() ,以连接每个pCheckBox。 As there are n pCheckboxes, pWidgets, pLayouts delete the 'self': 由于存在n个pCheckbox,pWidgets,pLayouts删除“自身”:

            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: 该错误在“ else”分支中,并且解决方法是为每个检查设置两个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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM