[Qt-interest] Virtual signals in base class?
Cyril C
cyril.mailing.list at gmail.com
Mon Sep 13 21:49:19 CEST 2010
Le 13/09/2010 21:16, Anders Bakken a écrit :
> On Thu, Sep 09, 2010 at 12:49:26AM -0700, Robert Caldecott wrote:
>> ...
>> signals:
>> virtual void someSignal(const QString& str, int n)
>> { ...
>> }
>> ...
>> };
>>
>> Now, Qt signals cannot have a body so I'm surprised this even compiles
>> (perhaps because the body is effectively empty). I also don't see the
>> point of making a signal virtual (as it can't have a body so how can
>> it be overridden?)
>>
>> Am I missing something here or is this code actually valid?
> My best guess is that this code is not moc'ed so the generated function
> is never compiled (or even generated).
My two cents: I once found very useful to declare a signal as virtual
(but still let the moc generate its implementation).
In a not so invasive way, this allowed me to customize the behavior of
the class by simply reimplementing it in a derived class like:
void Derived::someSignal(const QString &str)
{
// pre-emit hook (useful e.g. for mutating signal's arguments)
...
// emit
emit Super::someSignal(str2);
// post-emit hook (be careful: some slots may not have been invoked
yet)
...
}
More information about the Qt-interest-old
mailing list