Vladislav Ladenkov Vladislav Ladenkov - 3 months ago 27
C++ Question

How to restart QT app with Dialog Button

Here's the main.cpp. In one of the class'es scource code, I raise a QMessageBox and i want to restart the whole application(main function) if I push "yes"(Restart = open dialog, make setup, and get new GraphScene). To be completly clear, I want smth like:

if(QMeassageBox::Yes){qApp->restart()}

If thats not possbile with QMessageBox - what is the best way to do it? Thanks!

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ConfigurationDialog dlg;
dlg.exec();
dlg.show();

QGraphicsView gv;
QGraphicsScene gs;

PuzzlePiece item(PuzzlePiece::None, PuzzlePiece::Out, PuzzlePiece::In, PuzzlePiece::Out);

int cellSize = PIECE_SIZE;
QRect rect = item.boundingRect().toRect();
rect.translate(0.5 * cellSize + cellSize, 0.5 * cellSize + 5 * cellSize);

Logic logic;
QString P= ":/Image/Neyronnyie-seti.jpg";
logic.setup(dlg.puzzleSize(), P);


gs.addItem(&item);

gv.setScene(&logic);
gv.setWindowTitle("pazzles scheme");
gv.show();

return a.exec();
}

Answer

Put your application execution logic into a loop. Just remember that calling exec() multiple times is okay but destroying and re-creating application object itself may lead you to troubles on some platforms.

bool reallyQuit = false;

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    int exitCode = 0;

    // Global boolean here is just an example, you could break the loop some other way.
    while (!reallyQuit)
    {
        // Create dialogs etc.

        exitCode = app.exec();
    }

    return exitCode;
}

Then, just do qApp->quit() when you need to restart, directly or connecting QMessageBox'es accepted() signal to it.