tags:

views:

106

answers:

2
+2  Q: 

Virtual Qt signal?

Whilst reviewing some Qt C++ code I came across this:

class Foo
{
  Q_OBJECT

signals:
  virtual void someSignal(const QString& str, int n)
  {
    Q_UNUSED(str);
    Q_UNUSED(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 a valid code smell?

+1  A: 

That looks smelly to me.

It's valid to declare a signal in a base class and then emit it from a derived class, e.g.

class MyBase : public QObject
{
    Q_OBJECT
// ...
signals:
    void somethingHappened();
};

class MyDerived : public MyBase
{
    Q_OBJECT
// ...
    void doSomething();
};

void MyDerived::doSomething()
{
    // ....
    emit somethingHappened();
}

Maybe that's what the declaration in the question was meant to achieve.

Gareth Stockwell
A: 

Strictly C++ speaking it's normal it compiles, given signal is a macro for protected and Q_UNUSED is a cast to void. But you should get an error when running moc which precisely creates the implementation of the methods declared as signals.

gregseth