Results 1 to 6 of 6

Thread: Call a function at a dialog execution

  1. #1
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Call a function at a dialog execution

    I'm writing an application in which I need to open a dialog and execute automatically (without clicking on a push button for example) some animation function.

    For opening the dialog, the main windows has a push button that is connected to a slot, which contains

    Qt Code:
    1. //Open the dialog
    2. dialog dlg=new Dlg ;
    3. dlg.exec();
    4. //Get data from the dialog after it is closed
    5. ...
    To copy to clipboard, switch view to plain text mode 

    My dialog class is something like the following, where functionToBeCalled() is the function to be called:
    Qt Code:
    1. class Dlg : public QDialog
    2. {
    3. /* ...other attributes and methods here... */
    4.  
    5. public:
    6. void functionToBeCalled();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Could anybody tell me how to do that please?

    Thank you in advance for your help.

  2. #2
    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: Call a function at a dialog execution

    Either reimplement QDialog::exec() or QWidget::showEvent()
    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. The following user says thank you to wysota for this useful post:

    Nesbit (20th April 2012)

  4. #3
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call a function at a dialog execution

    Thank you wysota for your reply.

    But could you please be more specific?

    Someone suggested me with the following code:
    Qt Code:
    1. int Dlg::exec()
    2. {
    3. functionToBeCalled();
    4. return QDialog::exec();
    5. }
    To copy to clipboard, switch view to plain text mode 
    but the problem is that, my functionToBeCalled() is a function that does some animation (inside a QGraphicsView), so if we call this function before the exec() function, the user will not see the animation.
    I have got an idea but could not make it work. In the following code, I re-implement completely the exec() function, by just copying the source code of QDialog::exec() and adding functionToBeCalled() after show():
    Qt Code:
    1. int Dlg::exec()
    2. {
    3. Q_D(QDialog);
    4.  
    5. if (d->eventLoop) {
    6. qWarning("QDialog::exec: Recursive call detected");
    7. return -1;
    8. }
    9.  
    10. bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
    11. setAttribute(Qt::WA_DeleteOnClose, false);
    12.  
    13. d->resetModalitySetByOpen();
    14.  
    15. bool wasShowModal = testAttribute(Qt::WA_ShowModal);
    16. setAttribute(Qt::WA_ShowModal, true);
    17. setResult(0);
    18.  
    19. //On Windows Mobile we create an empty menu to hide the current menu
    20. #ifdef Q_WS_WINCE_WM
    21. #ifndef QT_NO_MENUBAR
    22. QMenuBar *menuBar = 0;
    23. if (!findChild<QMenuBar *>())
    24. menuBar = new QMenuBar(this);
    25. if (qt_wince_is_smartphone()) {
    26. QAction *doneAction = new QAction(tr("Done"), this);
    27. menuBar->setDefaultAction(doneAction);
    28. connect(doneAction, SIGNAL(triggered()), this, SLOT(_q_doneAction()));
    29. }
    30. #endif //QT_NO_MENUBAR
    31. #endif //Q_WS_WINCE_WM
    32.  
    33. bool showSystemDialogFullScreen = false;
    34. #ifdef Q_OS_SYMBIAN
    35. if (qobject_cast<QFileDialog *>(this) || qobject_cast<QFontDialog *>(this) ||
    36. qobject_cast<QWizard *>(this)) {
    37. showSystemDialogFullScreen = true;
    38. }
    39. #endif // Q_OS_SYMBIAN
    40.  
    41. if (showSystemDialogFullScreen) {
    42. setWindowFlags(windowFlags() | Qt::WindowSoftkeysVisibleHint);
    43. setWindowState(Qt::WindowFullScreen);
    44. }
    45. show();
    46.  
    47. /****** MY ANIMATION HERE *******/
    48. functionToBeCalled();
    49.  
    50. #ifdef Q_WS_MAC
    51. d->mac_nativeDialogModalHelp();
    52. #endif
    53.  
    54. QEventLoop eventLoop;
    55. d->eventLoop = &eventLoop;
    56. QPointer<QDialog> guard = this;
    57. (void) eventLoop.exec(QEventLoop::DialogExec);
    58. if (guard.isNull())
    59. return QDialog::Rejected;
    60. d->eventLoop = 0;
    61.  
    62. setAttribute(Qt::WA_ShowModal, wasShowModal);
    63.  
    64. int res = result();
    65. if (deleteOnClose)
    66. delete this;
    67. #ifdef Q_WS_WINCE_WM
    68. #ifndef QT_NO_MENUBAR
    69. else if (menuBar)
    70. delete menuBar;
    71. #endif //QT_NO_MENUBAR
    72. #endif //Q_WS_WINCE_WM
    73. return res;
    74. }
    To copy to clipboard, switch view to plain text mode 
    The first error I got is _'QDialog::d_func' : cannot access private member declared in class 'QDialog'_.
    Hope somebody can help.
    Have a nice day !

  5. #4
    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: Call a function at a dialog execution

    Quote Originally Posted by Nesbit View Post
    but the problem is that, my functionToBeCalled() is a function that does some animation (inside a QGraphicsView), so if we call this function before the exec() function, the user will not see the animation.
    The function should only start the animation and not "perform" it. I'm not going to explain it here how Qt event system works, you should read that in the docs.
    Last edited by wysota; 18th April 2012 at 10:09.
    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. The following user says thank you to wysota for this useful post:

    Nesbit (20th April 2012)

  7. #5
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call a function at a dialog execution

    Hmm... did you mean we cannot do what I wanted to do, wysota?
    I tried something like
    Qt Code:
    1. dialog.display();
    2. animation();
    To copy to clipboard, switch view to plain text mode 
    It works, but lacks some features I need from dialog.exec()

    P/s: You replied so quickly wysota, thank you so much

  8. #6
    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: Call a function at a dialog execution

    Qt is an event-driven framework. If you say "show me that dialog", it does not mean the dialog is actually shown at this particular moment in time. Instead an event is generated that at some point in future will be processed and will result in initializing surface for the widget and eventually showing it on screen. The same goes with animations -- an animation usually behaves in such a way that it changes some property (like position or color) of an object. If you want to visualize such a change, you call update() on the widget which again causes an event to be generated that at some later point in time is processed and the widget is redrawn. Some time later you have to change that property again to have an effect of smoothly changing that value. The way to do this is again through events, this time triggered by a timer that tells you a specified period of time has passed. Thus your "animation()" call doesn't actually perform the animation but only schedules the first frame of the animation and subsequent frames are scheduled and performed "some time later". Thus the order of calls (like starting the animation and calling exec) does not matter since both of them are not processed immediately.

    If you were thinking of doing the animation this way:

    Qt Code:
    1. for(int i=0;i<1000;++i) item->setPos(i, 200);
    To copy to clipboard, switch view to plain text mode 

    then obviously this will not work since by the time the widget is updated through events, the item will already have position set to (999,200) and thus you will only see the last frame of the animation.
    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.


  9. The following user says thank you to wysota for this useful post:

    Nesbit (22nd April 2012)

Similar Threads

  1. Javascript Function execution in html page
    By saeedIRHA in forum Newbie
    Replies: 6
    Last Post: 17th October 2011, 13:01
  2. How to call a dialog from a mainwindow
    By luiz4um in forum Qt Programming
    Replies: 26
    Last Post: 29th June 2010, 10:41
  3. Qt function call in vb.net
    By abghosh in forum Qt Programming
    Replies: 7
    Last Post: 6th March 2010, 17:00
  4. function call
    By Walsi in forum Qt Programming
    Replies: 3
    Last Post: 12th June 2007, 09:13
  5. how to call another dialog using menubar
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2007, 14:28

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.