PDA

View Full Version : QStringList in QObject::connect



DPinLV
6th September 2006, 04:11
Hello,

I'm trying to pass a QStringList using the signal slot, but when I try to use the QStringList in the slot it contains garbage (and exceptions). I set up the "connect" call to use QStringList* because otherwise the connect call fails.

QObject::connect (m_emitter_class,
SIGNAL(removeGamesSignal (int, QStringList* )),
m_pkr_receiver_class,
SLOT(invokeGamesRemoval (int, QStringList* )),
Qt::QueuedConnection);

I then tried the following as well:

QObject::connect (m_emitter_class,
SIGNAL(removeGamesSignal (int, QStringList& )),
m_pkr_receiver_class,
SLOT(invokeGamesRemoval (int, QStringList& )),
Qt::QueuedConnection);
This does not work, the error says:
QObject::connect: Cannot queue arguments of type 'QStringList&'

When I use Qt:: DirectConnection with the 'QStringList&' signature the QObject::connect suceeds.:confused:

It is possible to send a QStringList using a signal and Qt::QueuedConnection?

Thanks,
DP

munna
6th September 2006, 05:07
With queued connections, the parameters must be of types that are known to Qt's meta-object system (basically those understood by QVariant), because Qt needs to copy the arguments to store them in an event behind the scenes.

call qRegisterMetaType() (http://doc.trolltech.com/4.1/qmetatype.html#qRegisterMetaType) to register the data type before you establish the connection.

DPinLV
6th September 2006, 05:14
With queued connections, the parameters must be of types that are known to Qt's meta-object system (basically those understood by QVariant), because Qt needs to copy the arguments to store them in an event behind the scenes.

call qRegisterMetaType() (http://doc.trolltech.com/4.1/qmetatype.html#qRegisterMetaType) to register the data type before you establish the connection.

Thanks Munna,

There is no problem with doing this to Qt types (i.e. QStringList), this will not cause a conflict?

qRegisterMetaType<QStringList>("QStringList");

munna
6th September 2006, 05:18
There is no problem with doing this to Qt types (i.e. QStringList), this will not cause a conflict?

I don't think so. Give it a try.

jpn
6th September 2006, 06:37
...and the same issue has already been solved in thread: QStringList with Signal/Slot (http://www.qtcentre.org/forum/f-qt-programming-2/t-qstringlist-with-signalslot-2050.html).

wysota
6th September 2006, 10:40
Essentially you can't pass non-const references to a queued connection (as it doesn't make any sense). You'd have to use const QStringList& instead (but you wouldn't be able to change its contents). Using pointers is tricky too, because only a pointer is copied when the signal is emitted. If "in the meantime" (before the slot is executed) you delete the object, you'll end up with an invalid pointer and a possible segfault.

DPinLV
6th September 2006, 17:01
...and the same issue has already been solved in thread: QStringList with Signal/Slot (http://www.qtcentre.org/forum/f-qt-programming-2/t-qstringlist-with-signalslot-2050.html).
Thanks, I noticed there was an existing thread when I received my subscriber email from QT regarding this thread. After looking at the existing thread on this subject I was able to get it working. I use the following which succeeds:

qRegisterMetaType<QStringList>("QStringList");
QObject::connect (m_emitter_class,
SIGNAL(removeGamesSignal (int, const QStringList& )),
m_pkr_receiver_class,
SLOT(invokeGamesRemoval (int, const QStringList& )),
Qt::QueuedConnection);
I did not try the QVariant option mentioned in the previous thread since it seems like a matter of personal preference. If anyone disagrees please let me know why.


Essentially you can't pass non-const references to a queued connection (as it doesn't make any sense). You'd have to use const QStringList& instead (but you wouldn't be able to change its contents). Using pointers is tricky too, because only a pointer is copied when the signal is emitted. If "in the meantime" (before the slot is executed) you delete the object, you'll end up with an invalid pointer and a possible segfault.
Thanks for the explanation of what is going on under the covers with signals I appreciate that. I'm trying to learn what the Signals/Slots are doing as far as when they copy data, etc.
Thanks, wysota, jpn, and munna.