Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qdialog subclass: how to close upon confirmation
Forum Updated to NodeBB v4.3 + New Features

qdialog subclass: how to close upon confirmation

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 284 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andi456
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • A andi456

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 of accept/reject/done().

      A 1 Reply Last reply
      1
      • JonBJ JonB

        @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 of accept/reject/done().

        A Offline
        A Offline
        andi456
        wrote on last edited by
        #3

        @JonB Thanks, I've overlooked that I actually call exec() twice. That did the trick. Calling close is indeed not necessary.

        Kind regards,
        Andreas

        1 Reply Last reply
        0
        • A andi456 has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved