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. Deleting a modal dialog while it is in exec()

Deleting a modal dialog while it is in exec()

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.9k Views 2 Watching
  • 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.
  • c.savurC Offline
    c.savurC Offline
    c.savur
    wrote on last edited by
    #1

    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.

    kshegunovK 1 Reply Last reply
    0
    • c.savurC c.savur

      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.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @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.

      Read and abide by the Qt Code of Conduct

      c.savurC 1 Reply Last reply
      0
      • kshegunovK kshegunov

        @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.

        c.savurC Offline
        c.savurC Offline
        c.savur
        wrote on last edited by c.savur
        #3

        @kshegunov

        Callback register in the parent widget. The callback triggered externally and it can happen anytime.

        Your suggestion is that I need to emit a signal that invokes reject() slots?

        kshegunovK 1 Reply Last reply
        0
        • c.savurC c.savur

          @kshegunov

          Callback register in the parent widget. The callback triggered externally and it can happen anytime.

          Your suggestion is that I need to emit a signal that invokes reject() slots?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @c.savur

          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.

          Read and abide by the Qt Code of Conduct

          c.savurC 1 Reply Last reply
          0
          • kshegunovK kshegunov

            @c.savur

            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.

            c.savurC Offline
            c.savurC Offline
            c.savur
            wrote on last edited by
            #5

            @kshegunov said:

            @c.savur

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

            1 Reply Last reply
            0
            • kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              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
              }
              

              Read and abide by the Qt Code of Conduct

              c.savurC 1 Reply Last reply
              1
              • kshegunovK kshegunov

                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
                }
                
                c.savurC Offline
                c.savurC Offline
                c.savur
                wrote on last edited by
                #7

                @kshegunov

                Thank you for your help. I have emitted signal that trigger the reject().

                Have a wonderful day.

                1 Reply Last reply
                1

                • Login

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