【发布时间】:2016-07-11 13:01:46
【问题描述】:
我现在正在使用QFileDialog::getOpenFileName。但是,正如this article 中所建议的那样,当主应用程序在对话框打开时关闭时会崩溃。您可以在此处查看如何重现崩溃的示例:
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 I will crash!");
});
application.exec();
return 0;
}
我可以将QFileDialog 与普通构造函数一起使用,如here 所述。但是,然后我似乎没有得到原生 windows 文件打开对话框。
有没有办法通过 Qt 获得一个不崩溃的程序并使用原生 Windows 文件打开对话框?
【问题讨论】:
-
对话框打开时为什么主应用程序关闭?你不能阻止吗?
-
不,有一个单独的线程正在运行,可能会引发错误。在这种情况下,应用程序将关闭并且所有小部件都将被销毁。这工作正常(所有适当的析构函数等都被调用),除了仍在运行的 getOpenFileName。
-
你分配了父母吗? - 也可以发布你的代码来调用对话框,以防有任何明显的情况
标签: c++ qt qt5 fileopendialog qeventloop