问题与我的复选框在PyQt和Python [英] Issue with my Check Box in PyQt and Python

查看:788
本文介绍了问题与我的复选框在PyQt和Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为selectAllCheckBox的复选框。当处于选中状态时,listview中的所有复选框(动态创建的)都应该更改为选中状态,当selectAllCheckBox复选框处于未选中状态时,所有动态创建的复选框都应该更改为未选中状态。

  self.dlg.selectAllCheckBox.stateChanged.connect(self.selectAll)
def selectAll(self):
全选图层加载到listView

model = self.dlg.DatacheckerListView1.model()
用于范围内的索引(model.rowCount()):
item = model .item(index)
如果item.isCheckable()和item.checkState()== QtCore.Qt.Unchecked:
item.setCheckState(QtCore.Qt.Checked)

上面的代码使得listview中的动态复选框处于Checked状态,即使SelectAllCheckBox处于Unchecked状态。请帮助我如何解决这个使用python。在复选框被选中或取消选中以连接到槽而不是stateChanged时,在信号中是否可以执行任何操作?

解决方案

stateChanged 信号发送选中状态,因此广告位可以重写为:



> def selectAll(self,state = QtCore.Qt.Checked):
选择所有加载到listView中的图层 b
$ b model = self.dlg.selectAllCheckBox.model()
范围内的索引(model.rowCount()):
item = model.item(index)
如果item.isCheckable():
item.setCheckState(state)

列表视图中的所有行都有复选框, isCheckable 行可以省略)


I have a checkbox named "selectAllCheckBox". When this in checked state, all the checkboxes(created dynamically) inside the listview should change to checked state and when "selectAllCheckBox" checkbox is in Unchecked state, all the dynamically created checkboxes should change to unchecked state.

self.dlg.selectAllCheckBox.stateChanged.connect(self.selectAll)
def selectAll(self):
    """Select All layers loaded inside the listView"""

    model = self.dlg.DatacheckerListView1.model()
    for index in range(model.rowCount()):
        item = model.item(index)
        if item.isCheckable() and item.checkState() == QtCore.Qt.Unchecked:
            item.setCheckState(QtCore.Qt.Checked)

What the above code does it makes the dynamic checkboxes inside the listview to Checked state even when "SelectAllCheckBox" is in Unchecked state. Please help me how to solve this using python. Is there anything could be done in signal like when the checkbox is "checked" or "unchecked" to connect to a slot instead of stateChanged?

解决方案

The stateChanged signal sends the checked state, so the slot could be re-written as:

def selectAll(self, state=QtCore.Qt.Checked):
    """Select All layers loaded inside the listView"""

    model = self.dlg.selectAllCheckBox.model()
    for index in range(model.rowCount()):
        item = model.item(index)
        if item.isCheckable():
            item.setCheckState(state)

(NB: if all the rows in the list-view have checkboxes, the isCheckable line could be omitted)

这篇关于问题与我的复选框在PyQt和Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆