Problem using the if...else structure in main.cpp



  • Im trying to use the if else structure to control which form is shown before the main window. The app first checks if a file named add.txt is present then shows thecorrect form. Here's my code:

    @
    //a continuation of the main.cpp

    if (0 == access ("C:/Microsoft/add.txt",0))

    (MainWindow z;
    z.show();
    }

    else {
    window2 y;
    y.show();
    }

    return a.exec();
    }
    @

    However, the window just blinks and runs in the background with no UI. How do I make it run correctly by showing the UI??

    [EDIT: code formatting, please wrap in @-tags, Volker]


  • Moderators

    Both z and y are declared inside the block where you call show on them. Once you leave that block they are going out and are destroyed.


  • Moderators

    I would rewrite it as something like:
    @
    //a continuation of the main.cpp

    MainWindow *z = 0;
    window2 *y = 0;

    if (0 == access ("C:/Microsoft/add.txt",0))
    z = new MainWindow();
    z->show();
    } else {
    y = new window2();
    y->show();
    }

    return a.exec();
    }
    @

    Brain to terminal; ymmv


Log in to reply
 

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