qdialog subclass: how to close upon confirmation
-
Hi,
after creating a simple subclass of qdialog to hold several labels, a combobox and a lineedit. A button for confirming the user input and one for cancel, the dialog does not close after clicking the confirmation button once, but does so after a second click.
void AddJsonFieldDialog::on_OKBtn_clicked() { std::string feldname; if (!FieldNameEdt->text().isEmpty()) { QString fnqs = FieldNameEdt->text(); feldname = fnqs.toStdString(); } int feldtyp = FieldTypeCombo->currentIndex(); set_FieldType(feldtyp); set_FieldName(feldname); std::cout << "Dies ist der OKKnopf." << std::endl; // emit accepted(); //this->accept(); this->done(QDialog::Accepted); this->close(); }I've already tried to set Qt::WA_DeleteOnClose, but this just crashes the whole program.
The dialog is created and called from a button clicked slot like so:
code_textvoid MainWindow::on_AddFieldBtn_clicked() { AddJsonFieldDialog *dialog = new AddJsonFieldDialog(this); //dialog->setAttribute(Qt::WA_DeleteOnClose); if (dialog->exec()==QDialog::Rejected) { int ftype = dialog->get_FieldType(); std::cout << std::to_string(ftype) << ". Feldtyp" << std::endl; } else if (dialog->exec()==QDialog::Accepted) { int ftype = dialog->get_FieldType(); std::cout << std::to_string(ftype) << ". Feldtyp akzeptiert" << std::endl; //dialog->close(); } }Maybe this is not the best way to do it.
Any help will be appreciated.
Kind regards,
Andreas
-
Hi,
after creating a simple subclass of qdialog to hold several labels, a combobox and a lineedit. A button for confirming the user input and one for cancel, the dialog does not close after clicking the confirmation button once, but does so after a second click.
void AddJsonFieldDialog::on_OKBtn_clicked() { std::string feldname; if (!FieldNameEdt->text().isEmpty()) { QString fnqs = FieldNameEdt->text(); feldname = fnqs.toStdString(); } int feldtyp = FieldTypeCombo->currentIndex(); set_FieldType(feldtyp); set_FieldName(feldname); std::cout << "Dies ist der OKKnopf." << std::endl; // emit accepted(); //this->accept(); this->done(QDialog::Accepted); this->close(); }I've already tried to set Qt::WA_DeleteOnClose, but this just crashes the whole program.
The dialog is created and called from a button clicked slot like so:
code_textvoid MainWindow::on_AddFieldBtn_clicked() { AddJsonFieldDialog *dialog = new AddJsonFieldDialog(this); //dialog->setAttribute(Qt::WA_DeleteOnClose); if (dialog->exec()==QDialog::Rejected) { int ftype = dialog->get_FieldType(); std::cout << std::to_string(ftype) << ". Feldtyp" << std::endl; } else if (dialog->exec()==QDialog::Accepted) { int ftype = dialog->get_FieldType(); std::cout << std::to_string(ftype) << ". Feldtyp akzeptiert" << std::endl; //dialog->close(); } }Maybe this is not the best way to do it.
Any help will be appreciated.
Kind regards,
Andreas
@andi456 said in qdialog subclass: how to close upon confirmation:
if (dialog->exec()==QDialog::Rejected) {
} else if (dialog->exec()==QDialog::Accepted) {You are calling
QDialog::exec()twice, so the dialog is shown twice.//this->accept();
this->done(QDialog::Accepted);
this->close();Although it may not matter, if I recall correctly you don't need/want the
close()here --- just any one ofaccept/reject/done(). -
@andi456 said in qdialog subclass: how to close upon confirmation:
if (dialog->exec()==QDialog::Rejected) {
} else if (dialog->exec()==QDialog::Accepted) {You are calling
QDialog::exec()twice, so the dialog is shown twice.//this->accept();
this->done(QDialog::Accepted);
this->close();Although it may not matter, if I recall correctly you don't need/want the
close()here --- just any one ofaccept/reject/done(). -
A andi456 has marked this topic as solved on