Qt - how to toggle a QListWidgetItem by clicking on the text

qlistwidget item clicked python
qt qlistwidget
qlistwidget::finditems
qlistwidget sort
pyqt qlistwidget get selected item
qlistwidget add widget
removeitemwidget

You can toggle a QCheckBox by clicking on the label string. This gives the user a comfortably large target to click.

However, if I have a QListWidget with checkable items, the user must click inside the little box to toggle the item. This makes checking the items tedious and even a little painful for those of us with a Repetitive Stress Injury.

I tried connecting to the QListWidget itemClicked signal and toggling the item check box. That works fine if the user clicks on the item text. But if they actually click on the box, the check state gets toggled, then the itemClicked signal is sent, and code toggles the check state back to where it was.

Can anyone suggest a way to toggle the check state by clicking on either the checkbox or the item text?

Thanks...

QListWidgetItem doesn't provide any way to know where did user click, label or box, so the easiest way is to use QCheckBox as a widget. Now QCheckBox will behave as QCheckBox and user will be able to check box by clicking everywhere (on QCheckBox)

for(int r=0;r<7;r++)
{
    QListWidgetItem* lwi = new QListWidgetItem;
    ui->listWidget->addItem(lwi);
    ui->listWidget->setItemWidget(lwi, new QCheckBox(QString("checkBox%1").arg(r)));
}

Disadvantage: it can reduce performance if you have lot of items.

Check state change only when mouse clicks on checkbox, a qlistwidget in a main window, and I need to toggle the checkstate of a list widget item when clicking the checkbox before the item text. 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.

You can just keep the flag Qt::ItemIsUserCheckable unset and then the item's check state won't change when the checkbox gets clicked on. When then the itemClicked signal of the QListWidget is connected to a slot that toggles the check state the check state won't get toggled twice anymore.

The checkboxes of the items are shown when the item has a check state set no matter if they are checkable by the user.

QListWidget clicked signal, Default Re: QListWidget clicked signal. Reimplement To copy to clipboard, switch view to plain text mode. error: no matching function for call  I am trying to just click on an item in a list of items in a listwidget. I right clicked in my UI and went to the slot: void main::listWidget_itemClicked(QListWidgetItem* item) In there I can run commands ect But I want the selected item that I click on to be set to a String I tried using the CONNECT/SIGNAL route, but I came up empty there.

Here is the solution using python.

listWidget.itemPressed['QListWidgetItem*'].connect(lambda item: item.setCheckState(QtCore.Qt.Checked if item.checkState()==QtCore.Qt.Unchecked else QtCore.Qt.Unchecked))

Declaring this, before setting up your QlistWidget, or before adding your QlistWidget items seems to work well, and allows for an easy way to change the checkState of the default listWidget items.

Qt5 Tutorial QListWidget - 2020, In this tutorial, we'll populate the items of the widget list and then when a button is clicked, it will make some changes on the currently selected item (text color  A QListWidgetItem represents a single item in a QListWidget. Each item can hold several pieces of information, and will display them appropriately. The item view convenience classes use a classic item-based interface rather than a pure model/view approach. For a more flexible list view widget,

How to change the text color of an item in a QListWidget?, I am double clicking on an item and will like to change the color of it, then change it back once i double clicked http://doc.qt.io/qt-4.8/qlistwidgetitem.html#​setForeground From there it is trivial to toggle the color as needed. I want to change the text color of the items in QListWidget. For example, some items are in red text while others are in blue text. How do I do that? Thank you.

Qt Tutorials For Beginners 20 - QListWidget, In this Video I am going to show How to use QListWidget Class in QT if you click on one of Duration: 10:27 Posted: May 14, 2016 Qt - how to toggle a QListWidgetItem by clicking on the text. You can toggle a QCheckBox by clicking on the label string. This gives the user a comfortably large target to click. However, if I have a QListWidget with checkable items, the user must click inside the little box to toggle the item.

Qt 4.8: Qt Style Sheets Reference, List of Stylable Widgets. The following table lists the Qt widgets that can be customized using style sheets: The spacing property can be used to control the spacing between the text and indicator. an example. QListWidget, See QListView. QListWidgetItem *QListWidget:: itemAt (const QPoint &p) const. Returns a pointer to the item at the coordinates p. The coordinates are relative to the list widget's viewport(). QListWidgetItem *QListWidget:: itemAt (int x, int y) const. This is an overloaded function. Returns a pointer to the item at the coordinates (x, y).

Comments
  • Thanks @Chernobyl. I guess I had known about setting the item widget sometime in the past. I'll give it a try for my short lists. Not so sure about the architectural drawings I deal with that can have 100's of layer names...