Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between QObject::connect vs connect methods

I am a newbie with Qt. Most of the times Qt developers need to use signals and slots for object communication. I have seen two ways of connecting signals and slots so far.

1)QObject::connect(scrollBar, SIGNAL(valueChanged(int)),label,  SLOT(setNum(int)));

2)connect(scrollBar, SIGNAL(valueChanged(int)),label,  SLOT(setNum(int)));

What is the exact difference between both of them? Why do we have to prefix QObject in the first method?

like image 818
DesirePRG Avatar asked Jan 05 '14 10:01

DesirePRG


People also ask

Is QWidget a QObject?

All Qt widgets inherit QObject. The convenience function isWidgetType() returns whether an object is actually a widget. It is much faster than qobject_cast<QWidget *>(obj) or obj->inherits("QWidget"). Some QObject functions, e.g. children(), return a QObjectList.

How do I connect Qt signals and slots?

To connect the signal to the slot, we use QObject::connect(). There are several ways to connect signal and slots. The first is to use function pointers: connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed);

What is Q_emit?

Q_EMIT is simply a synonym for emit . This is useful when you're combining Qt code with other C++ code that also contains the identifier emit .


1 Answers

You call the static version in both aforementioned cases, the signature of which is as follows:

QMetaObject::Connection QObject::connect(const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection) [static]

When you are not connecting inside a QObject subclass, you will need to use the scoped variant, respectively, because you will not have an object in place to call it on. Here you can see some code representing the difference.

Not scoped

class MyClass : public QObject
{
    Q_OBJECT
    public:
        MyClass(QObject *parent) : QObject(parent) {
            connect(this, SIGNAL(mySignal()), SLOT(mySlot()));
        }

    public signals:
        void mySignal();

    public slots:
        void mySlot();
};

Scoped

int main(int argc, char **argv)
{
    QCoreApplication a(argc, argv);
    MyClass myObject;
    QObject::connect(&myObject, SIGNAL(mySignal()), &myObject, SLOT(mySlot()));
    return a.exec();
}

Please note that if you are trying to do this connection within the receiver object, you could even skip the third argument for convenience (i.e. less typing) because the non-static const version will take care of this automatically as per documentation:

QMetaObject::Connection QObject::connect(const QObject * sender, const char * signal, const char * method, Qt::ConnectionType type = Qt::AutoConnection) const

This function overloads connect().

Connects signal from the sender object to this object's method.

Equivalent to connect(sender, signal, this, method, type).

Every connection you make emits a signal, so duplicate connections emit two signals. You can break a connection using disconnect().

like image 173
lpapp Avatar answered Sep 24 '22 01:09

lpapp