无法使用自定义小部件更改可检查 QListViewItem 的状态 [英] Can't change state of checkable QListViewItem with custom widget

查看:52
本文介绍了无法使用自定义小部件更改可检查 QListViewItem 的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QListWidget,我想在其中添加一堆带有自定义小部件的项目:

I have a QListWidget where I want to add a bunch of items with a custom widget:

        listWidget = QListWidget()
        item = QListWidgetItem()
        item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
        item.setCheckState(Qt.Unchecked)
        listWidget.addItem(item)
        widget = MyLabelAndPushButton()
        item.setSizeHint(widget.sizeHint())
        listWidget.setItemWidget(item, widget)

顾名思义,MyLabelAndPushButton 只是一个在布局中包含 QLabel 和 QPushButton 的小部件.问题是我无法使用出现在小部件旁边的列表小部件中的复选框.它看起来完全正常,但当我点击它时没有任何反应.如果我使用 setItemWidget 删除该行,则它可以正常工作.我做错了什么?

As the name suggests MyLabelAndPushButton is just a widget containing a QLabel and a QPushButton in a layout. The problem is that I can not use the checkbox that appears in the listwidget next to the widget. It looks completely normal, but nothing happens when I click on it. If I remove the line with setItemWidget it works correctly. What am I doing wrong?

bugreports.qt.io/browse/QTBUG-16386 上报告了错误,但是得到回复API 不是为您打算做的事情而设计的"和如果您想显示自定义小部件,请使用 QListView 和子类 QItemDelegate."很明显,这不是错误,只是 API 无法处理的问题.

Reported bug at bugreports.qt.io/browse/QTBUG-16386 but got the reply "API is not designed for what you intend to do" and "In case if you want to display custom widget, use QListView and subclass QItemDelegate." So apparently it's not a bug, just something the API can't handle.

推荐答案

我不确定为什么在设置小部件时列表项不想改变其状态.我想解决此问题的方法是在您的小部件中添加一个复选框或连接到列表小部件的 itemClicked 信号并在那里重置项目的状态.请看看下面的例子是否适合你:

I'm not sure why exactly list item doesn't want to change its state when widget is set. I guess the workaround for this issue would be either adding a check box in your widget or connect to the listwidget's itemClicked signal and reset item's state there. Pls, see if an example below would work for you:

import sys
from PyQt4 import QtGui, QtCore

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        listWidget = QtGui.QListWidget()

        item = QtGui.QListWidgetItem()
        item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
        item.setCheckState(QtCore.Qt.Unchecked)
        listWidget.addItem(item)

        widget = QtGui.QCheckBox('test')
        item.setSizeHint(widget.sizeHint())
        listWidget.setItemWidget(item, widget)

        listWidget.itemClicked.connect(self.on_listWidget_itemClicked)

        self.setCentralWidget(listWidget)

    def on_listWidget_itemClicked(self, item):
        if item.listWidget().itemWidget(item) != None: 
            if item.checkState() == QtCore.Qt.Checked:
                item.setCheckState(QtCore.Qt.Unchecked)
            else:
                item.setCheckState(QtCore.Qt.Checked)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

希望对您有所帮助,问候

hope this helps, regards

这篇关于无法使用自定义小部件更改可检查 QListViewItem 的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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