Results 1 to 5 of 5

Thread: Can we connect QTimer::SingleShot with a slot taking arguments?

  1. #1
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Can we connect QTimer::SingleShot with a slot taking arguments?

    Hi all,

    I have a short question:

    Is it possible for this kind of connection to work:
    (Assume that all we need is defined)

    QTimer::singleShot(1000,this,SLOT(vSlot(2,3)));

    I know that it is possible to connect a QTimer::singleShot(...) with a slot not taking any args, but Can we do the same with a slot taking arg?
    Could you explain why this is not possible( or maybe is) ?

    Thank you.
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  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: Can we connect QTimer::SingleShot with a slot taking arguments?

    No, it is not possible. QObject::connect() does not save any state, you cannot map parameter values like that. Imagine it as a function call from the signal to the slot. The only way you can do it directly is the same way you can do it with a direct function call; with default parameter values:
    Qt Code:
    1. void vSlot(int foo = 2, int bar = 3);
    2.  
    3. QTimer::singleShot(1000,this,SLOT(vSlot()));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can we connect QTimer::SingleShot with a slot taking arguments?

    Hi,

    I suppose we have a small misunderstanding.
    We have this code:

    myClass.h
    Qt Code:
    1. ....
    2. private slots:
    3. void mySlot(int);
    4. ...
    To copy to clipboard, switch view to plain text mode 
    myClass.cpp
    Qt Code:
    1. ..
    2. void MyClass::mySlot(int _value){
    3. qDebug()<<_value;
    4. }
    5. ....
    To copy to clipboard, switch view to plain text mode 
    ..... and somewhere in the code.....
    Qt Code:
    1. int x = 3;
    2. QTimer::singleShot(2000,this,SLOT(mySlot(x)));
    3. ...
    To copy to clipboard, switch view to plain text mode 
    What I am trying to explain is this:
    In my app a QTimer::singleShot(...) do not work with slots taking arguments.

    And I would like to ask:
    Can a QTimer::singleShot() work with a slot taking arguments? yes, not? Why no?

    Kacper
    Last edited by jpn; 17th September 2008 at 18:06. Reason: missing [code] tags
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Can we connect QTimer::SingleShot with a slot taking arguments?

    No, as already said, you can NOT connect a signal to a slot that has MORE arguments than the signal. (It may have less, though.)
    The only exception (if you will) is when the missing arguments have defaults.

  5. The following user says thank you to caduel for this useful post:

    maverick_pol (17th September 2008)

  6. #5
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can we connect QTimer::SingleShot with a slot taking arguments?

    Thanks.

    I just forgot that using QTimer::singleShot(..) the signal sent is "timeout(...) taking no arguments and using the timeout interval". I was lucking this information.

    Clear enough.
    Last edited by maverick_pol; 17th September 2008 at 18:11.
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

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.