Search code examples
qtqt5pyqt5qlistwidgetselectionchanged

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?


Solution

  • 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();
        }
    }