PDA

View Full Version : Slots are called recursive or in infinite loop



Boron
17th August 2009, 17:53
Hello,
today I wanted to write a small tool that simply converts decimal to hex an vice versa as I type. Means: Every new digit in a QLineEdit instantly updates the converted number in the other lineEdit.
The decimalLineEdits textChanged() signal is connected to a slot UpdateDecimalChanged().
The hexLineEdits textChanged() signal is connected to a slot UpdateHexChanged().

In UpdateDecimalChanged() the decimal number is converted to a hex representation string an put in the hexLineEdit via setText(). The other slot accordingly.

But every setText() results in a new signal textChanged() which calls the slot, which updates the other lineEdit, which emit textChanged(), which calls the other slot, which updates the first lineEdit etc. etc.
This goes on forever.

Any idea how this infinite loop can be avoided?
I would prefer to avoid the usage of a "Convert" button. As I wrote: "convert as I type" is my goal.

adrian5632
17th August 2009, 20:20
Connect to the textEdited(const QString &) signal instead of textChanged.

Boron
18th August 2009, 18:11
Thanks a lot. This work perfectly.
I have to admit that I should have seen this.

axeljaeger
18th August 2009, 22:23
You can also cache the result from the last conversation and only do something if the value of the new calculation is different from the last one.

Boron
19th August 2009, 19:38
You mean only call setText() if the new conversion result differs from the last one?
Yes (*thinking*) this might also help.