PDA

View Full Version : QStringList with Signal/Slot



Sivert
3rd May 2006, 13:26
Hi, how can I send QStringList with signal ? I mean like:


signals:
void sig( QStringList & );
(...)
QStringList list;
emit sig( list );
But it won't work. I've read something about QVariant but can't get it to work either. Any suggestions ?

jpn
3rd May 2006, 13:36
You can either register QStringList as a meta type:

qRegisterMetaType<QStringList>("QStringList");

or use a QVariant holding a string list:

signals:
void sig( QVariant & );
(...)
QStringList list;
emit sig( QVariant(list) );

Sivert
3rd May 2006, 16:25
You can either register QStringList as a meta type:

qRegisterMetaType<QStringList>("QStringList");

or use a QVariant holding a string list:

signals:
void sig( QVariant & );
(...)
QStringList list;
emit sig( QVariant(list) );
First option doesn't seem to work :( . For the second I'm still getting empty QStringList.
When I use what you posted and add a slot.

void Main::sig( QVariant &var )
{
QStringListIterator i( var.toStringList() );
while ( i.hasNext() ) { qDebug() << i.next();}
qDebug() << "sig";
}Then this won't show anything but "sig", var is empty.

jpn
3rd May 2006, 17:47
This works fine for me:


#include <QObject>
#include <QVariant>
#include <QStringList>
#include <QCoreApplication>
#include <QtDebug>

class Sender : public QObject {
Q_OBJECT
public:
void send() {
QStringList stringList = (QStringList() << "a" << "b" << "c");
emit signal(QVariant(stringList));
}
signals:
void signal(QVariant &var);
};

class Receiver : public QObject {
Q_OBJECT
public slots:
void slot(QVariant &var) {
qDebug() << var.toStringList();
}
};

int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
Sender s;
Receiver r;
QObject::connect(&s, SIGNAL(signal(QVariant&)), &r, SLOT(slot(QVariant&)));
s.send();
}

#include "main.moc"


Output:


("a", "b", "c")

Sivert
3rd May 2006, 20:34
Works fine thank you. It was a problem with a function. I had a function that returned QStringList and it always return empty one. Dunno why but I changed a few things and it works nice.