Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QListWidget: change selection without triggering selectionChanged

I'm using a QListWidget to control and display some state.

Now to control the state, user-selection in the widget is used. to respond to that, I've connected the selectionChanged signal.

However the state can change by itsself and when that happens, I have a complete new state and want the selection to change.

To achieve that I'm iterating over the state and the items like this:

    for item, s in zip(items, state):
        item.setSelected(s)

However this triggers selectionChanged (even in every single iteration) I don't want that to happen at all.

is there another way to respond to the selection-change?

like image 312
IARI Avatar asked Dec 30 '25 22:12

IARI


1 Answers

You can simply use the QSignalBlocker class. Before calling a function which emits a signal, instantiate a QSignalBlocker object.

// ui->ListWidget is available.
{
    QSignalBlocker blocker( ui->ListWidget );
    for ( auto item : items )
    {
        item->setSelected();
    }
}
like image 139
p.i.g. Avatar answered Jan 01 '26 17:01

p.i.g.