vlad
vlad

Reputation: 343

Infinite loop on QTableWidget signal (cellChanged)

I have a signal coming from QTableWidget to slot where is the function recalculate(int, int). Based on the user input, the function does some calculations and changes the cell background color.

connect(ui->tableWidget_input, SIGNAL(cellChanged(int, int)), this, SLOT(recalculate(int, int)));

Problem is the cellChanged emits the signal when the background color is changed, I need the signal emission only when the text is changed.

The color change causes the infinite loop like this:

  1. Users changes the cell value, the signal is emitted.
  2. Recalculate(): When a condition is passed, the background is changed.
  3. When background is changed, Qt thinks the cell was changed and the signal is emitted.
  4. Recalculate(): When a condition is passed, the background is changed.
  5. Again and again into infinity.

Please, do you have any idea how to do signal emission by the text change only - no background color change?

Upvotes: 1

Views: 210

Answers (1)

eyllanesc
eyllanesc

Reputation: 243965

A simple solution is to block the emission of QTableWidget signals using blockSignals():

void Foo::recalculate(int row, int column){
    ui->tableWidget_input->blockSignals(true);
    // update here
    ui->tableWidget_input->blockSignals(false);   
}

Upvotes: 3

Related Questions