Results 1 to 14 of 14

Thread: Slot function's concurrent multiple executions

  1. #1
    Join Date
    Jan 2015
    Posts
    6
    Qt products
    Qt5

    Default Slot function's concurrent multiple executions

    Hi,

    In my application's (running on Android) main window (QMainWindow) I have a push button (QPushButton) whose signal 'clicked()' has been connected to a slot. In that slot function I am displaying a QMessageBox::about(...). The issue is that when I press that button again and again (in a very short duration) I see the message box popping up more than once. Even before the user gets rid of the first message box the second one pops up. I understand that this is because of multiple concurrent executions of the slot function. How should I manage this situation in QT? What I want is that the user should not be able to initiate another clicked() signal till the first call to slot function returns.

    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,546
    Thanks
    13
    Thanked 1,578 Times in 1,507 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Slot function's concurrent multiple executions

    On Windows and Linux the about() function gives you an application modal dialog. On OS X the dialog is not modal. On Android the docs do not say explicitly, but it looks like you have a non-modal dialog. If you do not want the button activated again then call setDisabled() on it when you enter the slot, and setEnabled() when you exit. If you want a modal dialog then construct your own and call exec() on it instead of using the convenience function. Bear in mind that there may be a good reason for the original non-modal dialog... but I do not know it.

  3. #3
    Join Date
    Jan 2015
    Posts
    6
    Qt products
    Qt5

    Default Re: Slot function's concurrent multiple executions

    Thanks for the reply. I tried two things: First as you suggested setDisabled/setEnabled and second disconnecting the signal-slot on entry and reconnecting on exit. These seem to work but I think there will be a race condition in both these solutions? One scenario for such a race is: first two clicks were so fast that both the slots were entered before the first one could complete the execution of setDisabled or disconnect.

    The QT documentation says "The about box has a single button labelled "OK". On Mac OS X, the about box is popped up as a modeless window; on other platforms, it is currently application modal.". I think for android also it should be application modal as only for OS X they have specifically mentioned it. Assuming that it is application modal, (as when the about box is displayed, I am not able to give input to the main window until I close the about box) I think what is happening is that before the first about message box is in displayed, the second click is already initiated causing another box to popup. order of events first_click->second_click->slot1->slot2. So even if I use an application modal dialog, the same issue may come up? ... Secondly, using the same code I am not able to reproduce the issue on Windows, I m not sure why.
    Last edited by MavenTux; 13th February 2015 at 21:10.

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

    Default Re: Slot function's concurrent multiple executions

    Quote Originally Posted by MavenTux View Post
    These seem to work but I think there will be a race condition in both these solutions?
    No, because there is only one thread involved.

    One scenario for such a race is: first two clicks were so fast that both the slots were entered before the first one could complete the execution of setDisabled or disconnect.
    No, that will never happen, there is no "concurrent execution" of the slots, they are serial.
    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.


  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,546
    Thanks
    13
    Thanked 1,578 Times in 1,507 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Slot function's concurrent multiple executions

    Secondly, using the same code I am not able to reproduce the issue on Windows, I m not sure why.
    You cannot reproduce it on Windows because the about() dialog is application modal on Windows: it blocks all input to the parent application while the dialog is active. You can do it on Android because the about() dialog is not modal: it does not block the parent application input so you can click the button multiple times and get multiple non-modal dialogs. I suspect the docs have not kept up with the expanded mobile platform support.

    To convince yourself of the serial nature of the slot executions in the single UI thread of execution can I suggest:
    Qt Code:
    1. void class::slot() {
    2. qDebug() << "In :" << Q_FUNC_INFO;
    3. // show non-modal dialog
    4. qDebug() << "Out:" << Q_FUNC_INFO;
    5. }
    To copy to clipboard, switch view to plain text mode 
    You will see an "Out" for the each "In" before you see another "In" (and before you click OK on the dialog I expect)... even when you click furiously.

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

    Default Re: Slot function's concurrent multiple executions

    Quote Originally Posted by ChrisW67 View Post
    even when you click furiously.
    Is that a formal name of a testing methodology?
    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.


  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,546
    Thanks
    13
    Thanked 1,578 Times in 1,507 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Slot function's concurrent multiple executions

    In some programs I have to deal with daily this is the only state of mind available to someone expecting sane behaviour from the program.

  8. #8
    Join Date
    Jan 2015
    Posts
    6
    Qt products
    Qt5

    Default Re: Slot function's concurrent multiple executions

    Thanks guys. As you mentioned the possibility of concurrency is not there as only one thread is involved. I'll try to implement a custom dialog with preferred modality for the same functionality. For using disable/enable strategy, I'll have to disable all the buttons in response to one click because furious clicking may be spread across multiple buttons. I don't want to do this.

  9. #9
    Join Date
    Jan 2015
    Posts
    6
    Qt products
    Qt5

    Default Re: Slot function's concurrent multiple executions

    I modified my slot function to
    Qt Code:
    1. void slotFunction()
    2. {
    3. QMessageBox message;
    4. message.setWindowModality(Qt::ApplicationModal);
    5. message.setWindowTitle("....");
    6. message.setDetailedText("....");
    7. message.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    But I see the same behavior on furious clicking of the button. Multiple message boxes pop up. "message.setWindowModality(Qt::ApplicationModa l)" doesn't seem to have any effect on android. What could be happening here?

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

    Default Re: Slot function's concurrent multiple executions

    It could be happening that Android does not support application modal dialogs. Why don't you disable the UI yourself as was already suggested?
    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.


  11. #11
    Join Date
    Jan 2015
    Posts
    6
    Qt products
    Qt5

    Default Re: Slot function's concurrent multiple executions

    I tried two things

    1. Created the messageBox in the constructor and assigned parent to the main window
    Qt Code:
    1. constructor()
    2. {
    3. messageBox = new QMessageBox(this);
    4. messageBox->setWindowModality(Qt::ApplicationModal);
    5. }
    6.  
    7. void slotFunction()
    8. {
    9. messageBox->setWindowTitle("....");
    10. messageBox->setDetailedText("....");
    11. messageBox->exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    This seems to solve the problem of multiple boxes opening. But still on furious clicking, flickering is observed in the message box as if one opened over another even though there is only one box opened in effect. Why should this behavior be there?

    2. disable/enable the UI. The components become grayed out on disable. Sometime I observe that on closing the message box, the components take a while to return from grayed out state to normal state, which looks odd. Is there a way to control how a widget looks in disabled state?

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

    Default Re: Slot function's concurrent multiple executions

    Quote Originally Posted by MavenTux View Post
    Is there a way to control how a widget looks in disabled state?
    Yes but that would be an overkill if you ask me.

    And your first approach is dangerous as you are execing the dialog while already in exec. Better set a flag before calling exec and bail out from the function if at the beginning the flag is already set.

    Qt Code:
    1. void slotFunction()
    2. {
    3. if(dialogExecuting) return;
    4. dialogExecuting = true;
    5. messageBox->setWindowTitle("....");
    6. messageBox->setDetailedText("....");
    7. messageBox->exec();
    8. dialogExecuting = false;
    9. }
    To copy to clipboard, switch view to plain text mode 
    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.


  13. #13
    Join Date
    Jan 2015
    Posts
    6
    Qt products
    Qt5

    Default Re: Slot function's concurrent multiple executions

    "you are execing the dialog while already in exec".. I don't understand this. The mentioned constructor is of the mainwindow and messagebox is a member of the mainwindow and slotFunction corresponds to a pushbutton in the mainwindow. MessageBox->setWindowModality(Qt::ApplicationModal) takes effect only when the messagebox's parent is set to the calling widget.

    To the first approach, I added disableUI before executing messagebox and enableUI after the messagebox is done. This works fine.. And regarding the disabled state of UI components, I was able to control them through the styleSheet (QPushButton:disabled {....}). But some other issues are observed. The moment I add a standardbutton say 'Ok' to the messagebox the BackKey which is supposed to quit the messagebox doesnt work. And if the text in the messagebox is too long and doesn't fit in the screen, the same can be viewed by scrolling. The issue is, to reach the 'Ok' button also I need to scroll down completely. It isn't visible on the screen until I scroll through the text completely. Weird behavior. Any ideas?

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

    Default Re: Slot function's concurrent multiple executions

    Quote Originally Posted by MavenTux View Post
    "you are execing the dialog while already in exec".. I don't understand this. The mentioned constructor is of the mainwindow and messagebox is a member of the mainwindow and slotFunction corresponds to a pushbutton in the mainwindow. MessageBox->setWindowModality(Qt::ApplicationModal) takes effect only when the messagebox's parent is set to the calling widget.
    There is an article on this on one of the KDE blogs.
    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: 2
    Last Post: 26th October 2013, 05:40
  2. Replies: 2
    Last Post: 26th August 2011, 08:51
  3. Replies: 4
    Last Post: 14th August 2011, 15:37
  4. My Slot Function
    By qtoptus in forum Qt Programming
    Replies: 3
    Last Post: 3rd November 2010, 16:38
  5. slot control of new function
    By tommy in forum Qt Programming
    Replies: 3
    Last Post: 11th November 2007, 03:58

Tags for this Thread

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.