Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 7

Qt-interest Archive, August 2006
avoiding signal loops


Message 1 in thread

What practices does everyone have for the classic problem of blocking
infinite signal loops created in situations like the following:

** keep two lists in sync **
QListWidget1 => itemSelectionChanged => QListWidget2 =>
itemSelectionChanged => QListWidget1 => itemSelectionChanged => etc...

I've used a noemit bool and just put the following at the beginning
and end of each slot:

if(noemit)
  return;
noemit = true;

...

noemit = false;

Does anyone have a cleaner solution? thanks


-- 
 [ signature omitted ] 

Message 2 in thread

> I've used a noemit bool and just put the following at the beginning
> and end of each slot:
>
> if(noemit)
>  return;
> noemit = true;
> ...
> noemit = false;
>
> Does anyone have a cleaner solution? thanks
Only emit when your state actually changes. Such that in your slot you 
should have:

public slots:
void setSomething(int something)
{
    if( m_something = something )
    {
       // No point doing anything....
       return;
    }

    m_something = something;
    emit somethingChanged(m_something);
}


begin:vcard
begin:vcard
fn:Justin Noel
n:Noel;Justin
org:(ICS) Integrated Computer Solutions ;Engineering
adr:;;54B Middlesex Turnpike;Bedford;MA;01730;USA
email;internet:justin@xxxxxxx
title:Senior Consulting Engineer / Certified Qt Instructor
tel;work:(617) 621-0060
x-mozilla-html:FALSE
url:http://www.ics.com
version:2.1
end:vcard


Message 3 in thread

Oops guys that ought to be:

public slots:
void setSomething(int something)
{
   if( m_something == something )
   {
      // No point doing anything....
      return;
   }

   m_something = something;
   emit somethingChanged(m_something);
}

//Must get more coffee...


Justin Noel wrote:
>
>> I've used a noemit bool and just put the following at the beginning
>> and end of each slot:
>>
>> if(noemit)
>>  return;
>> noemit = true;
>> ...
>> noemit = false;
>>
>> Does anyone have a cleaner solution? thanks
> Only emit when your state actually changes. Such that in your slot you 
> should have:
>
> public slots:
> void setSomething(int something)
> {
>    if( m_something == something )
>    {
>       // No point doing anything....
>       return;
>    }
>
>    m_something = something;
>    emit somethingChanged(m_something);
> }
>
>

begin:vcard
begin:vcard
fn:Justin Noel
n:Noel;Justin
org:(ICS) Integrated Computer Solutions ;Engineering
adr:;;54B Middlesex Turnpike;Bedford;MA;01730;USA
email;internet:justin@xxxxxxx
title:Senior Consulting Engineer / Certified Qt Instructor
tel;work:(617) 621-0060
x-mozilla-html:FALSE
url:http://www.ics.com
version:2.1
end:vcard


Message 4 in thread

>>> I've used a noemit bool and just put the following at the beginning
>>> and end of each slot:
>>>
>>> if(noemit)
>>>  return;
>>> noemit = true;
>>> ...
>>> noemit = false;
>>>
>>> Does anyone have a cleaner solution? thanks

What about calling QObject::blockSignals(bool) on the object you are about to change?  The logic is similar, but it is built into the Qt library.

-Andrew Vick

--
 [ signature omitted ]