我正在编写一个使用 QWidget::exec 返回值的应用程序,但似乎我无法正确关闭该类(我需要显式调用 Gate::~Gate 以删除该类)并且 QApplication::exec 永远不会退出。 Gate 是我的应用程序的主窗口
Gate::Gate(List *opciones, QWidget *parent):
QDialog(parent),
ui(new Ui::Gate)
{
ParseOption *ctmp;
int retvalue,i;
ui->setupUi(this);
validUser = false;
setAttribute(Qt::WA_QuitOnClose);
errno = 0; // no se de donde sale el error...
[...code...]
QObject::connect(ui->closeButton,&QAbstractButton::clicked,this,&QDialog::close);
QObject::connect(ui->passwordField,&QLineEdit::textChanged,this,&hellGate::enableopenButton);
QObject::connect(ui->openButton,&QAbstractButton::clicked,this,&hellGate::certificateUser);
QObject::connect(this,&hellGate::validateUser,this,&QDialog::done);
}
当mi程序调用时:
emit validateUser(QDialog::Accepted);
然后退出,但是关闭时 Gate 不调用析构函数,我在 main 中调用它但是带有标志 WA_QuitonClose 应自动关闭:
int main(int argc, char *argv[])
{
QWidgetList list;
QApplication a(argc, argv);
Gate w(configOptions);
if(w.exec() == QDialog::Accepted) {
w.~Gate();
qDebug("enter");
} else {
qDebug("No enter");
}
list = a.topLevelWidgets();
if(!list.isEmpty()) {
for(int i = 0;i<list.size();i++) {
qDebug("window: %i",list[i]->close());
}
} else {
qDebug("ALL closed");
}
return a.exec();
}
输出是“enter”(如果我调用 ~Gate,输出是“ALL closed”)。
我正在尝试让程序从“return a.exec()”行退出。 如果我不显式破坏 Gate a.topLevelWidget 返回一个带有 QWidget 的列表(我想那是 Gate)。 我需要调用 w.exec() 因为我需要 Gate 返回一个值并且 w.show() 声明为:
void show();
我需要调用 w.exec 并且当窗口 w(class Gate) 关闭时 a.exec 完成。 我做错了什么?
P.D 抱歉,如果文字难以理解,我的英语不是很好。
最佳答案
您创建了两个事件循环:
w.exec()
a.exec()
第二个事件循环在您关闭对话框之后开始,因此它将无限期地等待窗口关闭。
您可以为对话框调用 show()
或根本不使用 QApplication 事件循环:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Gate w(configOptions);
if(w.exec() == QDialog::Accepted)
{
qDebug("enter");
}
else
{
qDebug("No enter");
}
}
关于c++ - 关闭所有 QWidget 后 QApplication 不退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30904300/