Results 1 to 5 of 5

Thread: QStringList with Signal/Slot

  1. #1
    Join Date
    May 2006
    Location
    Poland
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QStringList with Signal/Slot

    Hi, how can I send QStringList with signal ? I mean like:
    Qt Code:
    1. signals:
    2. void sig( QStringList & );
    3. (...)
    4. emit sig( list );
    To copy to clipboard, switch view to plain text mode 
    But it won't work. I've read something about QVariant but can't get it to work either. Any suggestions ?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QStringList with Signal/Slot

    You can either register QStringList as a meta type:
    Qt Code:
    1. qRegisterMetaType<QStringList>("QStringList");
    To copy to clipboard, switch view to plain text mode 

    or use a QVariant holding a string list:
    Qt Code:
    1. signals:
    2. void sig( QVariant & );
    3. (...)
    4. emit sig( QVariant(list) );
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    Sivert (3rd May 2006)

  4. #3
    Join Date
    May 2006
    Location
    Poland
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStringList with Signal/Slot

    Quote Originally Posted by jpn
    You can either register QStringList as a meta type:
    Qt Code:
    1. qRegisterMetaType<QStringList>("QStringList");
    To copy to clipboard, switch view to plain text mode 

    or use a QVariant holding a string list:
    Qt Code:
    1. signals:
    2. void sig( QVariant & );
    3. (...)
    4. emit sig( QVariant(list) );
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. void Main::sig( QVariant &var )
    2. {
    3. QStringListIterator i( var.toStringList() );
    4. while ( i.hasNext() ) { qDebug() << i.next();}
    5. qDebug() << "sig";
    6. }
    To copy to clipboard, switch view to plain text mode 
    Then this won't show anything but "sig", var is empty.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QStringList with Signal/Slot

    This works fine for me:
    Qt Code:
    1. #include <QObject>
    2. #include <QVariant>
    3. #include <QStringList>
    4. #include <QCoreApplication>
    5. #include <QtDebug>
    6.  
    7. class Sender : public QObject {
    8. Q_OBJECT
    9. public:
    10. void send() {
    11. QStringList stringList = (QStringList() << "a" << "b" << "c");
    12. emit signal(QVariant(stringList));
    13. }
    14. signals:
    15. void signal(QVariant &var);
    16. };
    17.  
    18. class Receiver : public QObject {
    19. Q_OBJECT
    20. public slots:
    21. void slot(QVariant &var) {
    22. qDebug() << var.toStringList();
    23. }
    24. };
    25.  
    26. int main(int argc, char *argv[]) {
    27. QCoreApplication a(argc, argv);
    28. Sender s;
    29. Receiver r;
    30. QObject::connect(&s, SIGNAL(signal(QVariant&)), &r, SLOT(slot(QVariant&)));
    31. s.send();
    32. }
    33.  
    34. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Output:
    ("a", "b", "c")
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    Sivert (3rd May 2006)

  7. #5
    Join Date
    May 2006
    Location
    Poland
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStringList with Signal/Slot

    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.

Similar Threads

  1. QStringList
    By jaca in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2008, 10:12
  2. QStringList scope problem
    By ht1 in forum Qt Programming
    Replies: 5
    Last Post: 30th November 2007, 19:44
  3. QStringList in QObject::connect
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2006, 17:01
  4. Cannot queue arguments of type 'QStringList'
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2006, 20:48
  5. need help to classify some QStringList
    By patcito in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 21:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.