Call a function at a dialog execution

  • Hello everybody,

    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
    //Open the dialog
    dialog dlg=new Dlg ;
    //Get data from the dialog after it is closed

    My dialog class is something like the following, where functionToBeCalled() is the function to be called:
    @class Dlg : public QDialog
    /* ...other attributes and methods here... */

    void functionToBeCalled();

    Could anybody tell me how to do that please?

    Thank you in advance for your help.

  • As you are already subclassing QDialog you could probably overwrite the exec() function to call the functionToBeCalled();

    A (untested) solution could be:

    int Dlg::exec()
    return QDialog::exec();

    Another approach is to overwrite exec() to emit a signal that is connected to functionToBeCalled(). That would require functionToBeCalled to be defined as slot - or to use another slot to call the function...

  • Hi Daniel,

    Thank you for your reply.

    I have tested your 'untested' :D solution. Unfortunately, it is not what I need. Remember 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():

    @int Dlg::exec()

    if (d->eventLoop) {
        qWarning("QDialog::exec: Recursive call detected");
        return -1;
    bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
    setAttribute(Qt::WA_DeleteOnClose, false);
    bool wasShowModal = testAttribute(Qt::WA_ShowModal);
    setAttribute(Qt::WA_ShowModal, true);

    //On Windows Mobile we create an empty menu to hide the current menu
    #ifdef Q_WS_WINCE_WM
    #ifndef QT_NO_MENUBAR
    QMenuBar *menuBar = 0;
    if (!findChild<QMenuBar *>())
    menuBar = new QMenuBar(this);
    if (qt_wince_is_smartphone()) {
    QAction *doneAction = new QAction(tr("Done"), this);
    connect(doneAction, SIGNAL(triggered()), this, SLOT(_q_doneAction()));
    #endif //QT_NO_MENUBAR
    #endif //Q_WS_WINCE_WM

    bool showSystemDialogFullScreen = false;

    #ifdef Q_OS_SYMBIAN
    if (qobject_cast<QFileDialog *>(this) || qobject_cast<QFontDialog *>(this) ||
    qobject_cast<QWizard *>(this)) {
    showSystemDialogFullScreen = true;
    #endif // Q_OS_SYMBIAN

    if (showSystemDialogFullScreen) {
        setWindowFlags(windowFlags() | Qt::WindowSoftkeysVisibleHint);

    /****** MY ANIMATION HERE *******/

    #ifdef Q_WS_MAC

    QEventLoop eventLoop;
    d->eventLoop = &eventLoop;
    QPointer<QDialog> guard = this;
    (void) eventLoop.exec&#40;QEventLoop::DialogExec&#41;;
    if (guard.isNull())
        return QDialog::Rejected;
    d->eventLoop = 0;
    setAttribute(Qt::WA_ShowModal, wasShowModal);
    int res = result();
    if (deleteOnClose)
        delete this;

    #ifdef Q_WS_WINCE_WM
    #ifndef QT_NO_MENUBAR
    else if (menuBar)
    delete menuBar;
    #endif //QT_NO_MENUBAR
    #endif //Q_WS_WINCE_WM
    return res;

    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 !

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.