2

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?

IARI
  • 1,217
  • 1
  • 18
  • 35
  • 1
    You could block the signals, see [here](http://stackoverflow.com/questions/15633086/qt-block-temporarily-signals-between-2-qobjects) in C++. – Mel Jul 10 '15 at 13:56

1 Answers1

4

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();
    }
}
p.i.g.
  • 2,815
  • 2
  • 24
  • 41
  • thx, sounds promising 1. for how long is the signal blocked then? don't i have to release the blockage somehow? 2. python code? (i didnt mention it explicitly, but the tag i used an my code: im using pyqt5) – IARI Jul 10 '15 at 14:55
  • 3
    In C++ the blocker will be destroyed when it goes out of scope. Or you can use `ui->ListWidget->blockSignals( true );` and when you want signals again ui->ListWidget->blockSignals( false ); – p.i.g. Jul 10 '15 at 15:58