来自子 window 的 QFileDialog 关闭应用程序
QFileDialog from child window closes the application
我是 QT 新手,在从子 window 调用 QFileDialog 时遇到问题。我的应用程序相对简单。我有一个提示小部件,它获取用户输入然后在其父级上运行 show。
这是我的主。
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
ParentWidjet w(nullptr);
ChildWidget input(&w);
input.show();
return a.exec();
}
这是子窗口小部件的相关部分:
ChildWidget::ChildWidget(QWidget *parent) :
QDialog(parent),
ui(new Ui::InputPrompt){
ui->setupUi(this);
this->setParent(parent);
}
...
void ChildWidget::on_imagesSelect_clicked() {
inputFilepath = QFileDialog::getExistingDirectory(static_cast<ParentWidget *>(this->parent()), QCoreApplication::translate("main", "Open directory"), "./", QFileDialog::ShowDirsOnly);
ui->inputPath->setPlainText(inputFilepath);
std::cout << "y u exit" << std::endl;
}
//Setup and show the parent
void ChildWidget::on_buttonBox_accepted() {
static_cast<ParentWidjet *>(this->parent())->setup(inputFilepath, outputFilepath);
static_cast<ParentWidjet *>(this->parent())->show();
}
出于某种原因,当调用 QFileDialog
时,使用 OK
或 Cancel
关闭它会同时关闭父项和子项。如果我不使用它,而是单击调用 on_buttonBox_accepted()
函数的子项的 OK 按钮,则子项关闭并且父项小部件按预期出现。如果我不将父控件传递给主控件中的子控件,QFileDialog
将不再在 运行 时关闭子控件。我尝试将 QFileDialog 的父级更改为 this
或 nullptr
但这没有帮助。
我想我的问题类似于
或PyQt5 QFileDialog finishes app when called from child window
但我无法从他们那里提取适合我的解决方案。
长话短说,第二个 window 没有显示,也没有等待接收 accept 调用的操作,所以我的应用程序就死了。如果上次 window 关闭 属性 的退出是 disabled,则可以避免这种情况。 Qt 论坛提出了一个解决方案,以及一些更好的设计选择。这就是我最终选择的。
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
ParentWidget w(nullptr);
ChildWidget input;
if ( input.exec() == QDialog::accepted) // exec() blocks execution until ok/cancel. unlike show()
{
w.setup(input.inputFilepath, input.outputFilepath); // assuming these are public
w.show()
} else
{ // user closed or pressed cancelled
... what u want to do..
}
return a.exec();
}
我是 QT 新手,在从子 window 调用 QFileDialog 时遇到问题。我的应用程序相对简单。我有一个提示小部件,它获取用户输入然后在其父级上运行 show。 这是我的主。
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
ParentWidjet w(nullptr);
ChildWidget input(&w);
input.show();
return a.exec();
}
这是子窗口小部件的相关部分:
ChildWidget::ChildWidget(QWidget *parent) :
QDialog(parent),
ui(new Ui::InputPrompt){
ui->setupUi(this);
this->setParent(parent);
}
...
void ChildWidget::on_imagesSelect_clicked() {
inputFilepath = QFileDialog::getExistingDirectory(static_cast<ParentWidget *>(this->parent()), QCoreApplication::translate("main", "Open directory"), "./", QFileDialog::ShowDirsOnly);
ui->inputPath->setPlainText(inputFilepath);
std::cout << "y u exit" << std::endl;
}
//Setup and show the parent
void ChildWidget::on_buttonBox_accepted() {
static_cast<ParentWidjet *>(this->parent())->setup(inputFilepath, outputFilepath);
static_cast<ParentWidjet *>(this->parent())->show();
}
出于某种原因,当调用 QFileDialog
时,使用 OK
或 Cancel
关闭它会同时关闭父项和子项。如果我不使用它,而是单击调用 on_buttonBox_accepted()
函数的子项的 OK 按钮,则子项关闭并且父项小部件按预期出现。如果我不将父控件传递给主控件中的子控件,QFileDialog
将不再在 运行 时关闭子控件。我尝试将 QFileDialog 的父级更改为 this
或 nullptr
但这没有帮助。
我想我的问题类似于
或PyQt5 QFileDialog finishes app when called from child window
但我无法从他们那里提取适合我的解决方案。
长话短说,第二个 window 没有显示,也没有等待接收 accept 调用的操作,所以我的应用程序就死了。如果上次 window 关闭 属性 的退出是 disabled,则可以避免这种情况。 Qt 论坛提出了一个解决方案,以及一些更好的设计选择。这就是我最终选择的。
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
ParentWidget w(nullptr);
ChildWidget input;
if ( input.exec() == QDialog::accepted) // exec() blocks execution until ok/cancel. unlike show()
{
w.setup(input.inputFilepath, input.outputFilepath); // assuming these are public
w.show()
} else
{ // user closed or pressed cancelled
... what u want to do..
}
return a.exec();
}