Results 1 to 19 of 19

Thread: singleShot

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default singleShot

    I use the QTimer::singleShot function to delay for some seconds. If, that's the only thing, I set
    the SLOT parameter to NULL. Otherwise, I pass a SLOT function to it.

    As I checked the program, I think, the SingleShot function does not wait till the time pass and then carry on with
    the rest of the instructions. could you tell me how I can accomplish that? knowing that I cannot use the sleep() function at all as it would
    suspend all processes.

    Your

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: singleShot

    Qt Code:
    1. QTimer::singleShot(1000, &loop, SLOT(quit()));
    2. loop.exec();
    To copy to clipboard, switch view to plain text mode 

    However a much better idea is to just put the rest of your code in a separate slot and use the timer to call that slot.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: singleShot

    If you need that only for testing you can use: QTest::qSleep(1000); //one second (you will need CONFIG += qtestlib in your .pro file)

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: singleShot

    if I use QTImer with interval set, it keeps reseting and triggering the SLOT() infinitely. What I want is the SLOT get triggered only and only once, or even without any slots, set to NULL, for the sake of suspending the process.

    So, I guess this may get me to what I want
    QEventLoop loop;
    QTimer::singleShot(msec, &loop, NULL);
    loop.exec();

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: singleShot

    Quote Originally Posted by saman_artorious View Post
    if I use QTImer with interval set, it keeps reseting and triggering the SLOT() infinitely.
    QTimer::setSingleShot()
    What I want is the SLOT get triggered only and only once, or even without any slots, set to NULL, for the sake of suspending the process.
    Timers do not suspend anything. The code I gave you also doesn't suspend anything, it merely enters an event loop and exits it after a predefined period of time. However if during that time something happens (e.g. some timer fires) other slots and functions will be executed. If the code is part of some slot it might happen that this slot will be reentered again.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: singleShot

    In other words, let's consider this, whenever a particular condition sets to TRUE, I need to start a timer for a specific amount of time and then it goes dim till the condition goes true again.
    For this case, which one do yo think is better

    -connect the timer, set interval, set singleshot to true. and.. in this case I think everytime I need to activate the timer I should call restart

    the other case is to:
    -connect the timer, setinterval, and set the timer to start and stop when it times out.

    Considering that it should only run when the condition goes true.

    are the above correct?

    Quote Originally Posted by wysota View Post
    QTimer::setSingleShot()

    Timers do not suspend anything. The code I gave you also doesn't suspend anything, it merely enters an event loop and exits it after a predefined period of time. However if during that time something happens (e.g. some timer fires) other slots and functions will be executed. If the code is part of some slot it might happen that this slot will be reentered again.
    is there any way to wait for some period of time before executing the next instructions then? imagine I don't want to call any slots in some cases, I only want to delay.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: singleShot

    Quote Originally Posted by saman_artorious View Post
    In other words, let's consider this, whenever a particular condition sets to TRUE, I need to start a timer for a specific amount of time and then it goes dim till the condition goes true again.
    I don't understand what you mean. If you mean that you want it to fire once then make it a single shot timer by calling setSingleShot(true).

    For this case, which one do yo think is better

    -connect the timer, set interval, set singleshot to true. and.. in this case I think everytime I need to activate the timer I should call restart

    the other case is to:
    -connect the timer, setinterval, and set the timer to start and stop when it times out.
    The first one.

    is there any way to wait for some period of time before executing the next instructions then? imagine I don't want to call any slots in some cases, I only want to delay.
    Call sleep().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: singleShot

    It's not a good idea to use sleep(), as it would suspend all Timers n connections for a while. is there any other way to do it?

    if I use this
    Qt Code:
    1. QTimer::singleShot(msec, &loop, NULL);
    2. loop.exec();
    To copy to clipboard, switch view to plain text mode 

    will it stop the instruction execution or not.?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: singleShot

    is there any way to wait for some period of time before executing the next instructions then? imagine I don't want to call any slots in some cases, I only want to delay.
    That's what you wanted, wasn't it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: singleShot

    I don't know if you get my point or not. I cannot use sleep() as it will damage all the QTimers (suspends them all).
    I just want to delay for some seconds. And, I don't want to use as many slots to put the rest of my code inside it.

    sleep() suspends all running threads or timers. I don't want that, I only want to delay one particular thread.
    Last edited by saman_artorious; 9th June 2012 at 13:42.

  11. #11
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: singleShot

    sounds like you are stuck on trying to implement YOUR solution to a problem we don't know anything about.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  12. #12
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: singleShot

    consider the following:

    Qt Code:
    1. void FuelSystem::DSLFL_DataReceive(QByteArray data)
    2. {
    3. /* 4)Lower sensor 5)upper Sensor 6)water sensor */
    4.  
    5. QByteArray command_SV15;
    6. QByteArray command_HV14;
    7. QByteArray command_FTWL_SV9;
    8.  
    9. if(data.at(3) == FUEL_SYSTEM)
    10. {
    11. if(data.at(4) == 0x01)
    12. {
    13. FuelPump_start();
    14.  
    15. command_SV15.append(FUEL_SYSTEM);
    16. command_SV15.append(DSLFL_ST_SV_15);
    17. command_SV15.append(0x01);
    18.  
    19. emit DSLFL_SendToRS485(command_SV15);
    20. delay(_DELAY_MSEC);
    21. }
    22. else if(data.at(5) == 0x01)
    23. {
    24. FuelPump_stop();
    25.  
    26. command_HV14.append(FUEL_SYSTEM);
    27. command_HV14.append(DSLFL_HT_SV_14);
    28. command_HV14.append((char) 0x00);
    29.  
    30. emit DSLFL_SendToRS485(command_HV14);
    31. delay(_DELAY_MSEC);
    32. }
    33.  
    34. if((data.at(6) == 0x01) && (pumpStat == OFF))
    35. {
    36. command_FTWL_SV9.append(FUEL_SYSTEM);
    37. command_FTWL_SV9.append(DSLFLWaterEvacuate_SV_9);
    38. command_FTWL_SV9.append(0x01);
    39.  
    40. emit DSLFL_SendToRS485(command_FTWL_SV9);
    41. delay(_DELAY_MSEC);
    42.  
    43. waterEvacuateTimer->start();
    44. }
    45. }
    46. }
    47.  
    48. void FuelSystem::delay(int msec){
    49. QEventLoop loop;
    50. QTimer::singleShot(msec, &loop, NULL);
    51. loop.exec();
    52. }
    To copy to clipboard, switch view to plain text mode 

    As I send a packet to PLC, I need to delay for some time, waiting for the result of PLC operations. Here, I cannot use sleep() as it is a system call and it will suspend all other
    QTimers and the whole program. what I want is to only delay the current particular thread or object for 2 seconds everytime I connect to PLC.

    I hope this is clear now. : )

  13. #13
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: singleShot

    so you are just dealing with asynch io. This is not new problem and isn't solved best with blocking calls. Where is your code that you say 'needs to wait'?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: singleShot

    Quote Originally Posted by saman_artorious View Post
    sleep() suspends all running threads or timers. I don't want that, I only want to delay one particular thread.
    Sleep doesn't suspend any timers, those are handled by the operating system. Sleep suspends a thread. I have no idea why you want to delay anything at the places you marked in your code. Just put each if in a separate slot and upon ending one slot, start a timer to trigger the next one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: singleShot

    should he even be using timers? Looks like the code should be moved into slots, and called by something that is signalled when new data arrives.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: singleShot

    Quote Originally Posted by amleto View Post
    should he even be using timers?
    Probably not.
    Looks like the code should be moved into slots, and called by something that is signalled when new data arrives.
    That's right, provided he has some ability to be informed of data arrival.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: singleShot

    for example, if I want to command a valve to open via PLC. or let's say I want to switch on a pump, I need to delay for quite a small period of time, waiting for that valve to open in reality.
    then check via proximity switches whether it really got open or not after 2 seconds for example. That's why I need to delay for two seconds and the carry on with the rest of the instructions in that particular thread.
    if sleep() suspends threads then it's not a good idea to thread, I may use it in the above code to delay that thread, but it suspends all other threads as well.

    I have also used singleShot using SLOT, but in some cases I don not want to execute any SLOTS, just delaying for two sec.
    I think everything's now clear for you all. : )

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: singleShot

    Quote Originally Posted by saman_artorious View Post
    then check via proximity switches whether it really got open or not after 2 seconds for example.
    And if not?

    if sleep() suspends threads then it's not a good idea to thread,
    ???

    I may use it in the above code to delay that thread, but it suspends all other threads as well.
    Maybe you don't have other threads...

    I have also used singleShot using SLOT, but in some cases I don not want to execute any SLOTS, just delaying for two sec.
    Delay what?

    Usually if you want to delay something then you want this something to happen after that delay and not that you don't want anything to happen after that period of time.

    I'm leaning more and more to an opinion that you either have some misconception or misdesign in your program. To be honest, blocking anything in a reactive system such as one controlling some pump that can flood something seems to be a mistake on its own.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #19
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: singleShot

    Just my two cents: have you considered using a QSemaphore::tryAcquire() with timeout ?
    Qt Code:
    1. QSemaphore blocker;
    2. blocker.tryAcquire (1, delay);
    To copy to clipboard, switch view to plain text mode 
    That would block the calling thread and only it, with no interference with other threads, timers or whatever.
    Of course the thread would be dead to the outer world during the wait, so the GUI would better have to run somewhere else.

    As for polling an asynchronous device, I would suggest to create a dedicated thread instead of blocking the one issuing the command. But that's answering a question you did not ask, so...

Similar Threads

  1. QTimer::singleShot(0, ... confusion
    By bjoern83 in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2022, 10:30
  2. Replies: 3
    Last Post: 31st January 2010, 16:56
  3. Replies: 8
    Last Post: 10th December 2009, 10:06
  4. multiple QTimer::singleShot() calls?
    By mattc in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2009, 19:22
  5. Replies: 1
    Last Post: 6th April 2006, 12: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
  •  
Qt is a trademark of The Qt Company.