PDA

View Full Version : qApp->quit() can not work



xstream71
15th October 2013, 08:37
i create qdialog in my main window,
and dialog has two button ,ok and cancel

i hope when click cancel button ,my application can quit

in my dialog class,there are two signal :LoginOkClick & LoginCancelClick,
and main window has two slot to receive this two signal : StartTest & StopApp
qApp->quit() is in slot StopApp.

when i click cancel button ,application not quit , i ensure the api "qApp->quit()" was called.
But when i click ok (it will do dialog show & dialog exec again)and then click cancel ,my application quit success

can anyone tell me why?

My code
=======================MainWindow================= ===============

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

ui->setupUi(this);
mLoginDlg = new LoginDlg();
connect(mLoginDlg,SIGNAL(LoginOkClick()),this,SLOT (StartTest()));
connect(mLoginDlg,SIGNAL(LoginCancelClick()),this, SLOT(StopApp()));
mLoginDlg->exec();
}

MainWindow::~MainWindow()
{

delete ui;
}
//slots---------------------
void MainWindow::StopApp()
{


qDebug()<<"StopApp"<<endl;
qApp->quit();
}

void MainWindow::StartTest()
{

qDebug()<<"StartTest"<<endl;
mLoginDlg->show();
mLoginDlg->exec();
}


==========================LoginDlg================ ====================
LoginDlg::LoginDlg(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDlg)
{

ui->setupUi(this);
Init();
}

LoginDlg::~LoginDlg()
{

delete ui;
}


void LoginDlg::Init()
{

//slot init----------------------------------

QPushButton* bt_ok = ui->btbx_main->button(QDialogButtonBox::Ok);
QPushButton* bt_cancel= ui->btbx_main->button(QDialogButtonBox::Cancel);
connect(bt_ok,SIGNAL(clicked()),this,SLOT(onOkButt onClick()));
connect(bt_cancel,SIGNAL(clicked()),this,SLOT(onCa ncelButtonClick()));
}

void LoginDlg::closeEvent(QCloseEvent * e)
{

emit LoginCancelClick();
}

//slots---------------------
void LoginDlg::onOkButtonClick()
{


emit LoginOkClick();

}

void LoginDlg::onCancelButtonClick()
{

emit LoginCancelClick();
}

nix
15th October 2013, 09:45
quit() method use event mecanism, if it's do nothing the first call, your event loop is probably not running.
Show you main.c , exec() is a blocking function, so everything after your MainWindow constructor call will not be execute before the modal dialog is closed.

xstream71
15th October 2013, 09:56
Thanks.
I know now.