Signal a QTreeWidgetItem toggled checkbox

不问归期 提交于 2020-05-23 09:48:44

问题


How do I check if a checkbox is checked or unchecked? (In QTreeWidget) shows how to get the status of the checkbox on a qtreewidget item. Using that method, with a signal for itemClicked in the Tree, I can query if the selected item is checked or not. However, I then need to keep track of this item to see if it was previously checked or not.

Is there a method for knowing that a QTreeWidgetItem, which may be at different levels from the parent items, has a toggled checkbox, without making a subclass for it?

As a note, is there a specific reason the toggled method is not attached to the QTreeWidgetItem in QT?


回答1:


The itemClicked signal is not a good choice for handling treewidget checkboxes.

On the one hand, it gives false positives when not clicking on the checkbox part of an item; and on the other hand, it gives false negatives when the checkbox is toggled using the keyboard.

I think the best that can be done with the existing signals, is to use itemChanged. This will register all checkbox state changes made with both keyboard and mouse. It is not the perfect general solution, though, because it will give false positives whenever any other item data is changed (e.g. text, font, background colour, etc). So you would need to block the itemChanged signal whenever those other types of changes were made.



来源:https://stackoverflow.com/questions/27521391/signal-a-qtreewidgetitem-toggled-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!