Deleting a modal dialog while it is in exec()
-
Hello Everyone,
I have a problem that I could not come up with a good solution. So I want to ask your opinion about this problem.
The problem is that I have a desktop application that open a modal dialog which expects from user to press Yes/No. Everything is normal until this point. But I have registered a callback that, whenever this callback triggered I need to close the form and load another form. So I need to intervene exec() that to cancel the operation.
What is the best way to work around this problem?
Thank you in advance.
-
@c.savur
Hello,But I have registered a callback that, whenever this callback triggered I need to close the form and load another form.
How and where is this callback registered and how is it triggered?
What is the best way to work around this problem?
You can have a signal connected to the
reject()
slot of the dialog. -
Callback register in the parent widget. The callback triggered externally and it can happen anytime.
This is a bit vague. Is this callback executed from a separate thread? Who calls it? Could you provide some small snippet of code to illustrate what your setup is?
Your suggestion is that I need to emit a signal that invokes reject() slots?
Generally, yes, but without some more context I can't be sure if this would be working in your case.
-
@kshegunov said:
Callback register in the parent widget. The callback triggered externally and it can happen anytime.
This is a bit vague. Is this callback executed from a separate thread? Who calls it? Could you provide some small snippet of code to illustrate what your setup is?
Yes, callback triggered from another thread which is in the library. I do not have any control on it. I only can register my function so whenever my function executed ( get a callback), I need to stop everything and do something else.
Your suggestion is that I need to emit a signal that invokes reject() slots?
Generally, yes, but without some more context I can't be sure if this would be working in your case.
I have tried your solution and it works for a test application. I will apply the same idea to my application. I hope it will work :)
test application,
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);connect(&timer, SIGNAL(timeout()), this, SIGNAL(closeModal())); timer.start(5000);
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButtonExec_clicked()
{QMessageBox msgBox(QMessageBox::Question, tr("tilee"), tr("Close?"), QMessageBox::Yes|QMessageBox::No); connect(this, SIGNAL(closeModal()), &msgBox, SLOT(reject())); int reply = msgBox.exec(); if (!reply == QMessageBox::Yes) { return; } qDebug() << reply;
}
void MainWindow::on_pushButtonDelete_clicked()
{
emit closeModal();
} -
Yes, your example should work normally. However if the callback is called from another thread, which seems to be the case, I'd post the call to the
reject
slot as queued event:void callback() { QDialog * dialog; //< You need to have a valid reference to this object, this line is only for completeness QMetaObject::invokeMethod(dialog, "reject", Qt::QueuedConnection); //< This should call the reject slot in a thread safe manner }