Results 1 to 7 of 7

Thread: Calling a slot with arguments

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Calling a slot with arguments

    I need a slot to be called with an argument after Qt has done its internal bussiness and started the event loop again. The two options I see are:

    Qt Code:
    1. connect(this, SIGNAL(mysig(int)), someobject, SLOT(myslot(int)));
    2. emit mysignal(arg);
    To copy to clipboard, switch view to plain text mode 

    but that won't work because it calls mysignal right now instead of waiting for other scheduled stuff.


    Qt Code:
    1. QTimer::singleShot(0, someobject, SLOT(myslot()));
    To copy to clipboard, switch view to plain text mode 

    calls the slot at the proper time, but won't pass an argument. Anything put within the parentheses of myslot() is expected to be a type rather than a value.

    I suppose I could use the singleShot method and pass the argument via a global variable but that feels like being back to C64 Basic where GOSUB didn't take arguments...

    Is there a more elegant way?

  2. #2
    Join Date
    Jan 2008
    Posts
    25
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling a slot with arguments

    I think if you emit a signal it will be called after 'Qt does its internal business'.

    On QTimer: connect it to a slot of your own, which in turn calls myslot, but now with parameters. (like: void myslot_caller(){myslot(42); /*or some variable*/})

    Hope it helps.

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

    drhex (1st May 2008)

  4. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Calling a slot with arguments

    I think if you emit a signal it will be called after 'Qt does its internal business'
    .

    Not when I tried.. The "business" I'm talking about is drawing all the stuff that has been addWidget'ed since the main event loop was last run.

    Your other solution still does not have method and arguments in a combined package, but at least can use a class member instead of a global. I'll settle for that.

  5. #4
    Join Date
    Jan 2008
    Posts
    25
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling a slot with arguments

    You can specify a fifth argument to connect, which is a Qt::ConnectionType. Try Qt::QueuedConnection or Qt::BlockingQueuedConnection.

  6. #5
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Calling a slot with arguments

    The problem turned out to be worse than expected.
    What I have is a QScrollArea with plenty of small widgets in it. The user did something that necessitated killing and rebuilding those widgets. That works, but the scrollbar's position gets reset to 0. Therefore I read its position before redrawing and intended to restore it afterwards from that slot.

    Somehow, Qt was not finished drawing when the event loop started up again (i.e minimum() and maximum() of the QScrollBar are both zero). I had to call Qtimer::singleShot with a delay of 250ms to be allowed to move the scrollbar.

    Of course, I did not want to be dependent on that arbitrary value so I had the slot call itself every 100ms until the scrollbar's maximum() was sane.

  7. #6
    Join Date
    Jan 2008
    Posts
    25
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling a slot with arguments

    Killing the widgets and rebuilding them seems a bad idea to me, isn't it possible to do this a gentler way?

  8. #7
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Calling a slot with arguments

    Some of the widgets won't be there after the rebuild. I will probably change the code so that it kills only those and moves the others around to fill the holes.
    But doing that is more error-prone (new one could have been inserted too) than calling the already working kill&rebuild method I had.
    But rebuilding is slow, so I guess I'll have to implement the optimized version afterall.

Similar Threads

  1. How to declare SLOT as a parameter to member function?
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2018, 00:41
  2. Replies: 6
    Last Post: 21st September 2007, 13:51
  3. menu actions calling a single slot
    By jayw710 in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2007, 19:19

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.