Signals: Is this the expected behaviour of connect() ?

Eduardo Robles Elvira edulix at gmail.com
Sun Feb 24 22:53:42 GMT 2008


Hello people,

I myself have a quite twisted mind and like to find the boundaries of the 
things in order to understand them. And doing some Konqueror programming I've 
noticed an interesting Qt behaviour that can be shown with this sample code, 
which ends up printing "SIGNAL A WAS EMITTED":


// <code>
#include <QtCore/QDebug>
#include <QtCore/QObject>
#include <QtGui/QApplication>
 
class A
    : public QObject
{
    Q_OBJECT
 
public:
    A()
    {
        connect(this, SIGNAL(signalA()), SLOT(debugMe()));
    }
 
Q_SIGNALS:
    void signalA();
 
private Q_SLOTS:
    void debugMe()
    {
        qDebug() << "SIGNAL A WAS EMITTED";
    }
};
 
class B
    : public QObject
{
    Q_OBJECT
 
public:
    void tachan()
    {
        connect(this, SIGNAL(signalB()), new A(), SIGNAL(signalA()));
        signalB();
    }
 
Q_SIGNALS:
    void signalB();
};
 
int main(int argc, char **argv)
{
    QApplication app(argc, argv);
 
    B *b = new B();
    b->tachan();
 
    return app.exec();
}
 
#include "main.moc"
// </code>

This means that if you have a class A, I can trigger any signal of that class 
from within any other class that links to an A object.

Intuitively, this shouldn't work. signalA() should only be triggered by a 
given A class, or a friend class, or a parent class. That's the way I 
understand it, anyway. Or understood, until now =).

And indeed, it seems that signals were designed somehow  to be only triggered 
by it's owner (and friends & parents).  You cannot do emit new 
A()->signalA(); inside B. It doesn't even compile because the signal 
is "protected". But then, as the given code shows, connecting signalB() to 
signalA() and triggering signalA(), works fine - so maybe this is a bug?

Greetings,
         Eduardo Robles Elvira.
-- 
"The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man." (George Bernard Shaw)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20080224/323167c9/attachment.sig>


More information about the kde-core-devel mailing list