PDA

View Full Version : Showing a dialog at application startup



PaladinOfKaos
1st April 2008, 19:29
For my program, I need to pop up a dialog at first run so that the user can enter the directory where the data files are (they don't come with the program). I can look at my configuration file to decide whether or not to show the dialog - the question is where in my code to show it. If I put it in main() before app.exec(), the even loop won't have started yet, so it won't be responsive. I suspect there's an even handler in my main window class that I need to implement, but I can't figure out which one from the documentation.

The code path I think I need:
*Create main window
* run app.exec
* some even handler gets called
* show dialog

aamer4yu
1st April 2008, 19:50
In my opinion make ur own window class...
from main call the mainwindow. In the window class, keep a function to get input from the user as u expect. from the mainwindow constructor, call the function using QTimer::singleShot() .

This will display ur main window and also pop up the input dialog. After the input u can call some function to initialize ur data. This approach will help in case u later dont want to call the input dialog . You can still show the window without it.


Pseudo code :



main()
{
...
MainWindow window;
window.show();
return app.exec();
}


// mainwindow
class MainWindow:public QMainWindow
{
MainWindow()
{
...
QTimer::singleshot(getInput(),0); // call the input function using timer
}

};

void MainWindow::getInput()
{
// pop dialog
// initialize variables
}

wysota
1st April 2008, 19:51
A modal dialog spins its own event loop so you can put it in main().

PaladinOfKaos
1st April 2008, 20:05
The QTimer::singleShot method looks like it will do what I need. Thanks!