Cant change state of checkable QListViewItem with custom widget

2024/11/17 3:50:38

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)

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?

edit:

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.

Answer

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, QtCoreclass 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

https://en.xdnf.cn/q/71593.html

Related Q&A

Custom table for str.translate in Python 3

If I run this code:s.translate(str.maketrans({as: dfg, 1234: qw}))I will get:ValueError: string keys in translate table must be of length 1Is there a way to replace multiple characters at once using st…

Sending form data to aspx page

There is a need to do a search on the websiteurl = rhttp://www.cpso.on.ca/docsearch/this is an aspx page (Im beginning this trek as of yesterday, sorry for noob questions)using BeautifulSoup, I can get…

What is the difference between imregionalmax() of matlab and scipy.ndimage.filters.maximum_filter

I need to find the regional maxima of an image to obtain foreground markers for watershed segmentation. I see in matlab use the function imregionalmax(). As I dont have the matlab software, I use the f…

crontab: python script being run but does not execute OS Commands

I have this crontab configuration setup and the following script.MAILTO="[email protected]" 41 15 * * * /usr/bin/python /home/atweb/Documents/opengrok/setup_and_restart.py > /home/at…

concatenating arrays in python like matlab without knowing the size of the output array

I am trying to concatenate arrays in python similar to matlab array1= zeros(3,500); array2=ones(3,700); array=[array1, array2];I did the following in python:array1=np.zeros((3,500)) array2=np.ones((3,7…

How to save the result of a comparison using Djangos with template tag?

I would like to create new variable in django template, which will have a value of comparison obj.site.profile.default_role == objUnfortunately none of this code works: {% with obj.site.profile.default…

How to merge two list of dictionaries based on a value

I have two lists of dictionaries, lets say: a = [{id: 1, name: a}] b = [{id: 1, city: b}]I want to have a list that merges every dictionary in both lists with the same ID. In this example i expect to h…

How can I format strings to query with mysqldb in Python?

How do I do this correctly:I want to do a query like this:query = """SELECT * FROM sometable order by %s %s limit %s, %s;""" conn = app_globals.pool.connection() cur = con…

Doing many iterations of curve_fit in one go for piecewise function

Im trying to perform what are many iterations of Scipys curve_fit at once in order to avoid loops and therefore increase speed.This is very similar to this problem, which was solved. However, the fact …

python - dictionary iterator for pool map

I am handling set of frozensets. I am trying to find minimal sets for each frozenset in the dictionary output. I have 70k frozensets, so i am making chunk of this frozenset dictionary and parallelizing…