Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Check state change only when mouse clicks on checkbox
I am implementing 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. Does anyone know how to make the item checkstate change ONLY when the mouse clicks on the checkbox leading the item, instead of anywhere of the item?
Thank you very much!
@nebulaekg Without seeing some code I'm not 100% sure what you're trying to do, so there may be an easier way than my suggestion here...
You can handle left click for the entire list item, check to see if it's in the checkbox widget and process accordingly.
I feel like this is probably way overkill but I don't know what you already have and what you are trying to accomplish with the information you provided. I.e. how did you add the checkbox to the list widget? Share some code and someone can probably help you out more ..
@ambershark Below is how I dynamically add a listwidgetitem to the listwidget residing in the main app:
QFileInfo fs(f); QListWidgetItem * item = new QListWidgetItem(); ui->listItem->addItem(item); QLabel *s = new QLabel; s->setText(fs.baseName()); ui->listItem->setItemWidget(item, s); item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable); item->setCheckState(Qt::Checked);
The list widget can correctly show the items when I add the item, and is checkable. But my goal is: when I click the added item ONLY at its checkbox, its checkstate is toggled (checked -> unchecked, unchecked -> checked), not some other place. Currently, when I click anywhere that belongs to the item, the item's checkstate is toggled. This is not what I want, partially because I have reserved the doubleclick signal to some other important functionality. I wonder if there are any simple way to achieving this "checkbox-click-only" thing.
And an additional question, how to emit a signal corresponding to the click of the item's checkbox if the checkbox-click-only is achievable?
Thank you very much!
@nebulaekg Ok I get it now .. so this will be pretty complicated because you are using a QListWidget instead of QListView and a model.
My first recommendation is to use a QListView, a model, and delegates. Then you can control every aspect of your view including your checkbox and where the clicks matter.
If you want to stick with what you have then I think you will have to make a custom QListWidgetItem that handles the checkbox separately. It will probably be a lot of work to do that. It's definitely not the route I would take. Look into views/models/delegates since I think that will be a lot more of what you are wanting.
@ambershark It seems from the beginning I have chosen the wrong approach.... Thanks. I will look into it.
@nebulaekg It wasn't wrong, it was just the easy method. With easy comes very little customization. Now that you want to change it's behavior it becomes much harder. For simple lists you can definitely use the widget but for anything more you want a view/model and then delegates to control display.
Check into it I'm sure you'll find it's not that much harder than the widget and will give you the control you want. :)