[Qt-interest] Force the emission of a signal

Rohan McGovern rohan.mcgovern at nokia.com
Sun Mar 22 23:52:47 CET 2009


On Saturday 21 March 2009, ext Andrea Franceschini wrote:
> Hello everybody.
>
> Is it possible to force a QObject derived class to emit a
> particular signal, which of course is one of the signals of that
> class? Suppose you have the following:
>
> class MyObject : public QObject {
>    Q_OBJECT
> /* ... */
> signals:
>    void signal();
> };
>
> and suppose that the class itself has no way to know whether it
> is appropriate to emit that signal, instead the application in
> which it resides knows it perfectly. Is there a way to make the
> application make an instance of MyObject emit that signal? Or is
> it necessary to add a public emitSignal() method which is called
> by the application and simply emits the signal?

Assuming you have MyObject* obj:

if (!QMetaObject::invokeMethod(obj, "signal"))
  Q_ASSERT(0);

-- 
Rohan McGovern
Qt Software, Nokia



More information about the Qt-interest-old mailing list