Results 1 to 9 of 9

Thread: Signal/Slot execution sequences/preemption

  1. #1
    Join Date
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Signal/Slot execution sequences/preemption

    Hi,

    I have a widget that has a GraphicsScene and a QScrollBar. I have overloaded the widget's resizeEvent() function and connected the QScrollBar's sliderMove() to a custom slot. Each of these routines contains a function call that redraws the objects in my scene. I have found that if the SliderMove() slot executes while I am currently redrawing the scene it will "preempt" my first draw routine with it's call to the draw routine. It was my understanding that the slots would execute to completion (assuming you're not using threads) before another slot is allowed to execute.

    Example sequencing.

    Resize Event
    Start Draw...
    SliderMove
    Start Draw...
    Draw Complete.
    Draw Complete.

  2. #2
    Join Date
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal/Slot execution sequences/preemption

    I'm sorry, I meant to say I connected a custom slot to the QScrollBar's valueChanged() signal. My custom slot is called SliderMoved().

  3. #3
    Join Date
    Sep 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal/Slot execution sequences/preemption

    From Qt docs:
    Qt supports three types of signal-slot connections:
    • With direct connections, the slot gets called immediately when the signal is emitted. The slot is executed in the thread that emitted the signal (which is not necessarily the thread where the receiver object lives).
    • With queued connections, the slot is invoked when control returns to the event loop of the thread to which the object belongs. The slot is executed in the thread where the receiver object lives.
    • With auto connections (the default), the behavior is the same as with direct connections if the signal is emitted in the thread where the receiver lives; otherwise, the behavior is that of a queued connection.
    So changing your connection type to "queued" should do the trick.

  4. #4
    Join Date
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal/Slot execution sequences/preemption

    Well I'm not sure that's exactly what I want. Perhaps I don't fully understand how the GUI event loop works, but I would like my first Draw function call to run to completion before the GUI goes on and does other things (which is how i thought it worked with the default sig/slot mechanism). I know that sounds bad; probably not good for a having a responsive interface.

    Does the GUI execute and send signals in one thread, but execute slots in a different thread?

    I figured it might end up weird having the GUI continue to resize before all the draw functions have completed execution.

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

    Default Re: Signal/Slot execution sequences/preemption

    Slots or events will not be disturbed during execution. The effect you obtain has to be caused by some other thread of yours running or a faulty interpretation of results you get. Events and signals are executed synchronously (in the same thread as the object they belong to).
    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
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal/Slot execution sequences/preemption

    Well I added Qt::QueuedConnection in the connect() call for the valueChange() signal from the QScrollBar and it appears to have solved the problem.

    I'm not quite sure I understand why though....

    So the GUI is up and running, I resize it and it fires a resizeEvent which eventually calls my overloaded function which calls my Draw() function. At the same time the scroll bar fires an event saying valueUpdate() because the scroll bar was stretched so it now has a "new" value. That signal is immediately emitted and caught by the slot which also tries to call my Draw() routine.

    So now with Queued Connections the valueUpdated() signal is not emitted until the GUI finishes executing the resizeEvent() and essentially goes "idle" ?

    If I have my widget (set as the central widget) inside a MainWindow and my QScrollBar is part of the MainWindow's status bar....exactly how many threads do I have running?

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

    Default Re: Signal/Slot execution sequences/preemption

    Signal is always emitted immediately... The slot might be called later, if you're using queued connections. But if the latter solves your problems, it means you're using threads in your application. Are you?

    If I have my widget (set as the central widget) inside a MainWindow and my QScrollBar is part of the MainWindow's status bar....exactly how many threads do I have running?
    If you're not spawning any threads yourself, you should have exactly one thread running.
    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
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal/Slot execution sequences/preemption

    Ok, so then I don't understand why does the resizeEvent() function not run to completion before the slot function is processed? I figured everything would have to happen serially. Does QT actually abort event processing to handle an incoming signal? Why is QT processing the QScrollBar signals while it's suppose to be in the middle of processing a resize event?

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

    Default Re: Signal/Slot execution sequences/preemption

    Quote Originally Posted by xenome View Post
    Ok, so then I don't understand why does the resizeEvent() function not run to completion before the slot function is processed?
    Maybe some signal is emitted from within the event handler?

    Does QT actually abort event processing to handle an incoming signal?
    No, nothing gets aborted. But if a signal is emitted from within the event, a call will immediately be made to process the signal. When that's done, the event will continue.

    Why is QT processing the QScrollBar signals while it's suppose to be in the middle of processing a resize event?
    Don't have a clue, that's your program...
    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.


Similar Threads

  1. Replies: 12
    Last Post: 9th April 2008, 13:49
  2. What if not Signal/Slot
    By munna in forum General Discussion
    Replies: 2
    Last Post: 26th May 2006, 06:48

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.