Pregunta

Im a student programmer using Qt to build and application for work and I am having some difficulty figuring out how to handle when a user has exited a Dialog without saving changes. The dialog is primarily purposed for data entry so being able to identify if changes have been made and then offering the user the option to save before quitting would be extremely usefull. I looked through Qt's Documentation on QDialog and didn't find anything in regards to the mode that is returned if the exit button is pressed. Having a means to just identify when the exit button is clicked is first priority. Also, being a student programmer I am also open to any ideas for best practices in regards to how I should compare before and after data. The data is entered into a table so I am guessing that I would have to make something like this

connect(some kinda exit handler, SIGNAL(clicked), this, SLOT(comparePreAndPostTable)

QVector<QString> prechanges =  everything from the table
QVector<QString> postchanges = everything from table when exit is clicked


if(prechanges != postchanges)
{
     Give oppertunity to save
}

Any assistance is appreciated! It would really be nice if Qt already had something for this!

¿Fue útil?

Solución

You need to override the closeEvent() method of QWidget to handle the exit button of your dialog. The documentation even give an example that strangely looks like what you are looking for:

 void MainWindow::closeEvent(QCloseEvent *event)
 {
     if (maybeSave()) {
         writeSettings();
         event->accept();
     } else {
         event->ignore();
     }
 }

where maybeSave() would compare to see if anything has changed.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top