سؤال

I've created a simple class inherited from QObject and created a signal, like this:

testobject.h:

#ifndef TESTOBJECT_H
#define TESTOBJECT_H

#include <QObject>

class testObject : public QObject
{
    Q_OBJECT
public:
    explicit testObject(QObject *parent = 0);

signals:
    somethingChanged();

public slots:

};

#endif // TESTOBJECT_H

testobject.cpp:

#include "testobject.h"

testObject::testObject(QObject *parent) :
    QObject(parent)
{
}

And then tried to make an QEventLoop that should stop when the signal is emitted:

testObject *foo = new testObject;
QEventLoop loop;
connect(foo, SIGNAL(somethingChanged()), loop, SLOT(quit()));
loop.exec();

But compiler complains like this:

error: no matching function for call to 'TestObjectHere::connect(objectTest&, const char*, QEventLoop&, const char*)'

How should I fix this?

هل كانت مفيدة؟

المحلول

I think you need a pointer to QObject:

connect(foo, SIGNAL(somethingChanged()), &loop, SLOT(quit()));

edit: the error message complains about types that are not visibile from your sample:

error: no matching function for call to 'TestObjectHere::connect(objectTest&, const char*, QEventLoop&, const char*)'

also objectTest& should be changed to a pointer, instead of a reference. In the code posted, foo is a pointer, then you should have a mismatch between actual code passed to compiler and what you show.

Try to act similarly on source, for instance, prepend the pointer to operator, as we have done for target.

نصائح أخرى

Combine the two: CapelliC 's answer and my comment:

QObject::connect(foo, SIGNAL(somethingChanged()), &loop, SLOT(quit()));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top