Results 1 to 4 of 4

Thread: Passing arguments

  1. #1
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Passing arguments

    Hi,
    I understood it's not possible to pass extra arguments within a SLOT function.

    So, given the following code:
    Qt Code:
    1. QTimer::singleShot(PollTimeout, this, SLOT(myFunction()));
    To copy to clipboard, switch view to plain text mode 

    Could someone be so kind to teach me how to pass arguments to the myFunction() wrapped in the SLOT?
    Thanks

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing arguments

    That is not possible. I suggest you store the information in the data members of the object whose slot will be executed. If you cannot do that (e.g. if there are several timers calling myFunction and you need to identify which one timed out), you can, instead of using QTimer::singleShot(), instanciate QTimers and use QSignalMapper to bind values (integers or pointers) to their timeout() signals to identify them.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing arguments

    I understood it's not possible to pass extra arguments within a SLOT function.
    The function signature for your slot must match the function signature of the signal it is connected to, with the exception that the slot can have fewer arguments than the signal by dropping arguments from the end of the argument list. That is, if your signal has arguments (int, float, double), then valid slots could have the signatures (), (int), (int, float), and (int, float, double). But a slot with signature (int, float, double, char *) would be rejected at connect time.

    The way around this is usually to create a slot that is connected to the first signal, and in that slot emit a second signal that has the correct arguments. You need to store the values for the additional arguments somewhere, typically as member variables of the class that handles the first signal.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Passing arguments

    Thank you very much for the suggestions!

Similar Threads

  1. Replies: 1
    Last Post: 7th September 2010, 15:49
  2. Replies: 12
    Last Post: 7th September 2010, 12:53
  3. Replies: 3
    Last Post: 23rd February 2010, 04:33
  4. Replies: 1
    Last Post: 3rd February 2010, 04:45
  5. passing arguments in SLOT
    By eleanor in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2008, 21:55

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.