How to identify the checked checkbox in a PySide2 QListWidget

I'm trying to find the checked item in a QListWidget, however, I'm not sure how to identify the one being checked because I'm not using a name for each individual checkbox. Basically, I have multiple unknown strings, which are made into checkboxes and put into a QListWidget. I am then trying to find which one of these checkboxes has been checked.

This is how I am currently making my checkboxes (but I can use another widget if it is better)

thing2 = ['asdsa', 'dasdas', 'sdasdas', 'asdsa', 'sdasdad']
for item in thing2:
    vbox.addWidget(QCheckBox(item))

and it creates an application that looks like this: vbox with multiple stacked checkboxes

However, I can't tell which checkbox is being checked (because I want to perform an action on checked items)

So far, I've tried using the QListWidget in order to better organize it, and I had originally thought I'd be able to refer to each of the items as with normal python lists, however I still wasn't able to check which checkbox was ticked.

https://github.com/yjg30737/pyqt-checkbox-file-list-widget

This is basically what I'm trying to do, picking some of the checkboxes, and have a button which allows me to delete the items, however, I haven't been able to make it work with normal text. (I checked the installed files, and I think it only works with files)

I've also tried using Model View Controller code, which was recommended to me online as it was said to be a lot easier to have it transferred onto a widget, but I found it extremely confusing (and it didn't work out) so I'd prefer not using that if its possible.

Are there any methods that I can use instead for my program to recognize state changes in ways that allow me to modify the changed items? I'm ok if it completely changes the code.

EDIT: I think my question was really confusing but below is the code I used to create the list widget. I couldn't use QCheckBox and put it directly into the QListWidget, because I don't think it accepts it, I got the following error when I tried to the code:

item = QListWidgetItem(QCheckBox("Hi"))
list_widget.addItem(item)
  PySide2.QtWidgets.QListWidgetItem(PySide2.QtGui.QIcon, str, typing.Optional[PySide2.QtWidgets.QListWidget] = None, int = PySide2.QtWidgets.QListWidgetItem.ItemType.Type)
  PySide2.QtWidgets.QListWidgetItem(typing.Optional[PySide2.QtWidgets.QListWidget] = None, int = PySide2.QtWidgets.QListWidgetItem.ItemType.Type)
  PySide2.QtWidgets.QListWidgetItem(PySide2.QtWidgets.QListWidgetItem)
  PySide2.QtWidgets.QListWidgetItem(str, typing.Optional[PySide2.QtWidgets.QListWidget] = None, int = PySide2.QtWidgets.QListWidgetItem.ItemType.Type)

So instead of using a checkbox, I used ItemIsUserCheckable.

This is a minimum reproducible example of my code:

import sys

from PySide2.QtCore import Qt
from PySide2.QtWidgets import QApplication, QComboBox, QMainWindow, QListWidget, QHBoxLayout, QListWidgetItem, QWidget, \
    QPushButton, QCheckBox
from PySide2 import QtCore, QtWidgets

app = QApplication()
widget = QListWidget()
for i in range(5):
    item = QListWidgetItem(str(i))

    item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
    item.setCheckState(QtCore.Qt.Checked)
    widget.addItem(item)
    item = QListWidgetItem(QCheckBox("Hi"))
    widget.addItem(item)

selected = []
for i in range(0, widget.count()):
    if widget.item(i).isChecked():
        selected.append(i)

widget.show()
app.exec_()

(I am trying to delete the checked item)

Upvotes: 0

Views: 611

Answers (1)

ableitung
ableitung

Reputation: 198

OnClick:

self.selected = []
for i in range(0, self.listWidget.count()):
    if(self.listWidget.item(i).isChecked()):
       self.selected.append(i)

This will return all selected IDs to selected.

Upvotes: -1

Related Questions