Results 1 to 5 of 5

Thread: Slots are called recursive or in infinite loop

  1. #1
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Slots are called recursive or in infinite loop

    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.
    Attached Images Attached Images

  2. #2
    Join Date
    Aug 2009
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Slots are called recursive or in infinite loop

    Connect to the textEdited(const QString &) signal instead of textChanged.

  3. #3
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slots are called recursive or in infinite loop

    Thanks a lot. This work perfectly.
    I have to admit that I should have seen this.

  4. #4
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Slots are called recursive or in infinite loop

    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.
    It's nice to be important but it's more important to be nice.

  5. #5
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slots are called recursive or in infinite loop

    You mean only call setText() if the new conversion result differs from the last one?
    Yes (*thinking*) this might also help.

Similar Threads

  1. Replies: 7
    Last Post: 6th March 2008, 18:24
  2. Infinite loop - resize parent from child
    By bitChanger in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2006, 13:21
  3. is it possible to stay on a user defined infinite loop?
    By mahe2310 in forum Qt Programming
    Replies: 9
    Last Post: 24th March 2006, 14:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.