Results 1 to 3 of 3

Thread: [SOLVED] share data between caller and slot

  1. #1
    Join Date
    Jan 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [SOLVED] share data between caller and slot

    hi.
    my need is to pass data between a method that calls an async request and the slot that is connected to the relative signal.

    an example to make this clear:

    Qt Code:
    1. void foo()
    2. {
    3. QString str1 = "str1";
    4.  
    5. connect(requestManager, SIGNAL(requestFinished()), this SLOT(slot2()));
    6.  
    7. this->requestManager->startRequest(); //emits requestFinished()
    8. }
    9.  
    10. void slot2()
    11. {
    12. //here i need str1 from foo
    13. }
    To copy to clipboard, switch view to plain text mode 

    requestManager is an unmodifiable class.
    one way is to save str1 as a member of the class, but i'm wondering if there is a better way to do this.


    thanks in advance for any answer
    Last edited by whites11; 24th April 2010 at 13:35.

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: share data between caller and slot

    your requestManager is a QObject inherited, that's why you can add a property to it and retrieve it within ANY slot (not only in your class)
    Qt Code:
    1. const char * MY_PROPERTY_NAME = "property name";
    2. void foo()
    3. {
    4. QString str1 = "str1";
    5.  
    6. requestManager->setProperty( MY_PROPERTY_NAME, str1 );
    7.  
    8. connect(requestManager, SIGNAL(requestFinished()), this SLOT(slot2()));
    9.  
    10. this->requestManager->startRequest(); //emits requestFinished()
    11. }
    12.  
    13. void slot2()
    14. {
    15. //here i need str1 from foo
    16. QString str1 = sender()->property( MY_PROPERTY_NAME ).toString();
    17. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: share data between caller and slot

    thanks man, that's exactly what i was searching for!

Similar Threads

  1. Fill a data record in the beforeInsert slot
    By graciano in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 18:36
  2. disconnect SIGNAL/SLOT directly after emitting data
    By donglebob in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2009, 22:53
  3. What is the best way to share data between 2 programs?
    By ModeZt in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2008, 21:23
  4. Help me to use QtSharedMemory to share the data objects
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 11:50

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.