c++ - Run QFileDialog::getOpenFileName without separate event loop? -
i'm using qfiledialog::getopenfilename right now. however, suggested in this article, crashes when main application closes while dialog open. can see example of how reproduce crash here:
int main(int argc, char **argv) { qapplication application{argc, argv}; qmainwindow *main_window = new qmainwindow(); main_window->show(); qpushbutton *button = new qpushbutton("press me"); main_window->setcentralwidget(button); qobject::connect(button, &qpushbutton::clicked, [main_window]() { qtimer::singleshot(2000, [main_window]() { delete main_window; }); qfiledialog::getopenfilename(main_window, "close me fast or crash!"); }); application.exec(); return 0; }
i can use qfiledialog
normal constructor instead, described here. however, don't seem native windows file open dialog.
is there way non crashing program , use native windows file open dialog through qt?
if close main_window
instead of deleting it, won't crash.
by way, check if there qfiledialog
opened avoid wrong app exit.
in next example, i'm closing dialog, implement solution:
#include <qtimer> #include <qapplication> #include <qmainwindow> #include <qpushbutton> #include <qfiledialog> #include <qdebug> int main(int argc, char **argv) { qapplication application{argc, argv}; qmainwindow *main_window = new qmainwindow(); main_window->show(); qpushbutton *button = new qpushbutton("press me"); main_window->setcentralwidget(button); qobject::connect(button, &qpushbutton::clicked, [main_window]() { qtimer::singleshot(2000, [main_window]() { qobjectlist list = main_window->children(); while (!list.isempty()) { qobject *object= list.takefirst(); if (qobject_cast<qfiledialog*>(object)) { qdebug() << object->objectname(); qfiledialog* filedialog = qobject_cast<qfiledialog*>(object); filedialog->close(); } } main_window->close(); }); qfiledialog::getopenfilename(main_window, "close me fast or crash!"); }); application.exec(); return 0; }
Comments
Post a Comment