Qt/C++:检查 QListWidgetItem 时发出信号?
在我的表单中,我有一个 QListWidget
,其中包含可检查的 QListWidgetItems
。我正在寻找一种方法来捕获 QListWidgetItem
被选中/取消选中的事件。我没有看到任何这样的信号存在,但也许我错了。我目前正在做的是使用 QListWidget::itemClicked()
信号并检查 QListWidgetItem
的 checkState
,但这不是我想要什么,因为每次单击该项目时都会发生此事件,而不仅仅是切换复选标记。有人可以提供一些帮助吗?谢谢!
In my form I have a QListWidget
which contains checkable QListWidgetItems
. I'm looking for a way to capture the event of a QListWidgetItem
being checked/unchecked. I don't see any such signal existing for this but maybe I'm wrong. What I'm currently doing is using the QListWidget::itemClicked()
signal and checking the checkState
of the QListWidgetItem
, but this isn't what I want because this event happens any time the item is clicked, not just went the checkmark is toggled. Can anyone give some assistance? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然没有提供这样的信号,最好的选择是使用
QListWidget:: itemChanged(QListWidgetItem* item)
,并扫描生成的item->checkState()
。与使用itemClicked
相比,这应该略有改进Apparently no such signal is provided, your best bet is to use
QListWidget::itemChanged(QListWidgetItem* item)
, and scan the resultingitem->checkState()
. This should be a slight improvement over usingitemClicked
另一个选项是将您自己的 QAbstractListModel 与 QListView 结合使用。首先,这确实添加了一些额外的代码,因为您需要添加自己的管理代码。但您确实可以获得较低级别的访问权限。基本上是因为
QListView
会询问您的模型要做什么。它还会将输入转发回您的列表模型,您可以在其中连接它。或者,您可以子类
QStandardItemModel
并捕获与更改复选框相关的某些编辑。An extra option is to use your own
QAbstractListModel
with aQListView
. At first this does add some extra code as you need to add your own management code . But you do get a lower level access. Basically because theQListView
will ask your model what to do. It will also relay input back to your listmodel where you can hook into it.Alternatively you could subclass
QStandardItemModel
and catch certain edits related to changing the checkbox.