Reputation: 157447
I am trying to connect to a signal in this way:
QObject::connect(myObj, SIGNAL(mySignal(std::list<MyClass*> myList)), this, SLOT(mySlot(std::list<MyClass*> myList)));
the slot is not invoked. Is that wrong? Can I use std::list
in a signal/slot
pair?
EDIT: same pair without parameters work
class TestThread : public QThread
{
Q_OBJECT
public:
.....
protected:
virtual void run();
private:
std::list<MyClass*> myList;
signals:
void mySignal(std::list<MyClass*>&);
};
Q_DECLARE_METATYPE (std::list<MyClass*>)
EDIT:
void mySlot(const std::list<MyClass*> &);
void
MyManager::mySlot(const std::list<MyClass*> &theList)
{
std::cout << "mySlot " << std::endl;
}
void mySignal(const std::list<MyClass*> &theList);
TestThread ::TestThread (std::list<MyClass*>&theList, QObject *parent)
: QThread(parent), myList(theList)
{
}
void
TestThread ::run()
{
...
emit mySignal(myList);
}
in the end:
QObject::connect(threadObj, SIGNAL(mySignal(std::list<MyClass*>)), this, SLOT(mySlot(std::list<MyClass*>)));
Upvotes: 2
Views: 6644
Reputation: 12693
You only need the type, not the actual name of the variable.
QObject::connect(myObj, SIGNAL(mySignal(std::list<MyClass*>)), this, SLOT(mySlot(std::list<MyClass*>)));
If you were trying to send an int
, you would use
SIGNAL(mySignal(int))
Not
SIGNAL(mySignal(int x))
Edit: As @Chris points out, mismatching the const
and &
shouldn't make a difference.
The code below correctly emits the signal and receives it in the other thread. If you don't use qRegisterMetaType
, you get a runtime warning message telling you to do so.
class A
{
public:
A(): i(1) {}
int i;
};
class T : public QThread
{
Q_OBJECT
public:
void run()
{
emit mySignal(myList);
}
std::list<A*> myList;
signals:
void mySignal(const std::list<A*>&);
};
Q_DECLARE_METATYPE (std::list<A*>)
class Test: public QDialog
{
Q_OBJECT//this macro flags the class for the moc tool
public:
Test()
{
qRegisterMetaType<std::list< A* > >("std::list<A*>");
t = new T;
connect(t, SIGNAL(mySignal(std::list<A*>)), this, SLOT(mySlot(std::list<A*>)));
printf("after connect\n");
t->start();
}
public slots:
void mySlot(const std::list<A*>& list){printf("mySlot");}
protected:
T* t;
};
Upvotes: 4
Reputation: 749
If the signal emiting object and the receiving object are located in different threads, you have to register your meta-type with qRegisterMetaType before using it in an emit. In your case:
qRegisterMetaType<std::list< MyClass* > >("std::list<MyClass*>");
somewhere at the start of your app should do it.
Upvotes: 7
Reputation: 22346
If you have derived from QObject
, added the Q_OBJECT
macro to the class header, and made sure the signals and slots involved have the correct access modifiers (i.e. public slots
) - then it may mean that you need to add:
#include <QMetaType>
Q_DECLARE_METATYPE( std::list<MyClass*> );
Upvotes: 1