关闭所有QWidget

时间:2015-06-17 23:48:23

标签: c++ qt5

我正在编写一个使用QWidget :: exec返回值的应用程序,但似乎我无法正确关闭该类(我需要显式调用Gate :: ~Gate来删除该类)并且QApplication :: exec永远不会退出。 门是我申请的主要窗口

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);

然后退出,但是当关闭时不要调用析构函数,我在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();
}

输出是&#34;输入&#34; (和&#34;全部关闭&#34;如果我打电话~Gate)。

我正在尝试退出该行的程序&#34;返回a.exec()&#34;。 如果我没有明确地破坏Gate,a.topLevelWidget会返回一个带有QWidget的列表(我想这是Gate)。 我需要调用w.exec()因为我需要Gate返回一个值而w.show()声明为:

void show();

我需要调用w.exec并且当windows w(类Gate)关闭时a.exec完成。  我做错了什么?

P.D对不起,如果文字难以理解,我不太懂英语。

1 个答案:

答案 0 :(得分:0)

您创建了两个事件循环:

  1. w.exec()
  2. a.exec()
  3. 第二个事件循环在 关闭对话框后启动,因此它将无限期地等待窗口关闭。

    您可以为对话框调用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");
        }
    }
    
相关问题