Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How To Close only a QMessageBox by using Cancel option in QT ??

How To Close only a QMessageBox by using Cancel option in QT ??

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 4 Posters 7.2k 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.
  • K Offline
    K Offline
    Kistlak
    wrote on last edited by Kistlak
    #1

    I am creating WordPad using C++ by using QT framework. And I have created mainwindow.cpp and mainwindow.h files. Then I have created TextCreator.cpp and TextCreator.h files. Still, I am very new to QT and C++. So, I don't know lots of things.

    I have created a Message box for save button. As usual there are 3 options which are Yes , No & Cancel. Yes and No options okay.

    But , I need to fighure out when someone click Cancel option , how to close only the QMessageBox. How can I do it ??

    Here is my code..

    void TextCreator::on_actionSave_triggered()
    {
        closemsg = new QMessageBox;
        closemsg->setText("Your Text Has Some Changes !!");
        closemsg->setInformativeText("Do You Want to Save It ?");
        closemsg->setStandardButtons(QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
        closemsg->setDefaultButton(QMessageBox::Yes);
        int ret=closemsg->exec();
    
        switch(ret)
        {
    
        case QMessageBox::Yes:
    
            if(TextFile==NULL)
            {
                on_actionSave_As_triggered();
            }
    
            else
            {
                on_actionSave_triggered();
            }
            break;
    
        case QMessageBox::No:
    
            ui->textEdit->close();
            exit(0);
            break;
    
        case QMessageBox::Cancel:
    
            QMessageBox msgBox;
            msgBox.exec();
            break;
    
        //default:
            //break;
        }
    
        this->close();
    }
    
    K 1 Reply Last reply
    0
    • K Kistlak

      I am creating WordPad using C++ by using QT framework. And I have created mainwindow.cpp and mainwindow.h files. Then I have created TextCreator.cpp and TextCreator.h files. Still, I am very new to QT and C++. So, I don't know lots of things.

      I have created a Message box for save button. As usual there are 3 options which are Yes , No & Cancel. Yes and No options okay.

      But , I need to fighure out when someone click Cancel option , how to close only the QMessageBox. How can I do it ??

      Here is my code..

      void TextCreator::on_actionSave_triggered()
      {
          closemsg = new QMessageBox;
          closemsg->setText("Your Text Has Some Changes !!");
          closemsg->setInformativeText("Do You Want to Save It ?");
          closemsg->setStandardButtons(QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
          closemsg->setDefaultButton(QMessageBox::Yes);
          int ret=closemsg->exec();
      
          switch(ret)
          {
      
          case QMessageBox::Yes:
      
              if(TextFile==NULL)
              {
                  on_actionSave_As_triggered();
              }
      
              else
              {
                  on_actionSave_triggered();
              }
              break;
      
          case QMessageBox::No:
      
              ui->textEdit->close();
              exit(0);
              break;
      
          case QMessageBox::Cancel:
      
              QMessageBox msgBox;
              msgBox.exec();
              break;
      
          //default:
              //break;
          }
      
          this->close();
      }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Kistlak

      Hi and welcome to devnet forum

      I am not sure what you mean the message box shall be close with any of those three buttons. At least the first one.

      However, you are generating a memory leak with your code. With

      closemsg = new QMessageBox;
      

      you are assigning some memory presumably to a pointer which is part of declaration of TextCreator. Typically one would expect that you are deleting the memory before exiting this routine. It is not obvious why you want to keep the memory for some other use.

      Therefore, probably it is better to allocate and remove the memory for the QMessageBox.

      void TextCreator::on_actionSave_triggered()
      {
           QMessageBox *closemsg = new QMessageBox;
           closemsg->setText("Your Text Has Some Changes !!");
           closemsg->setInformativeText("Do You Want to Save It ?");
           closemsg->setStandardButtons(QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
           closemsg->setDefaultButton(QMessageBox::Yes);
           int ret=closemsg->exec();
       
           closemsg->deleteLater();   // this will free the memory from the event loop.
      
           switch(ret)
           {
       
           case QMessageBox::Yes:
       
               if(TextFile==NULL)
               {
                   on_actionSave_As_triggered();
               }
       
               else
               {
                   on_actionSave_triggered();
               }
               break;
       
           case QMessageBox::No:
       
               ui->textEdit->close();
               exit(0);                                  // that is a brute force exit of your whole program. Most developers do like this.
               break;
       
           case QMessageBox::Cancel:
       
               QMessageBox msgBox;        // this is another way, which could be used directly for the main message box as well. Nor requirement to delete later
               msgBox.exec();                        // you are having another message box here. That is open and waiting. 
               break;
       
           //default:
               //break;
           }
       
           this->close();
       }
      

      Vote the answer(s) that helped you to solve your issue(s)

      K 1 Reply Last reply
      4
      • K koahnig

        @Kistlak

        Hi and welcome to devnet forum

        I am not sure what you mean the message box shall be close with any of those three buttons. At least the first one.

        However, you are generating a memory leak with your code. With

        closemsg = new QMessageBox;
        

        you are assigning some memory presumably to a pointer which is part of declaration of TextCreator. Typically one would expect that you are deleting the memory before exiting this routine. It is not obvious why you want to keep the memory for some other use.

        Therefore, probably it is better to allocate and remove the memory for the QMessageBox.

        void TextCreator::on_actionSave_triggered()
        {
             QMessageBox *closemsg = new QMessageBox;
             closemsg->setText("Your Text Has Some Changes !!");
             closemsg->setInformativeText("Do You Want to Save It ?");
             closemsg->setStandardButtons(QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
             closemsg->setDefaultButton(QMessageBox::Yes);
             int ret=closemsg->exec();
         
             closemsg->deleteLater();   // this will free the memory from the event loop.
        
             switch(ret)
             {
         
             case QMessageBox::Yes:
         
                 if(TextFile==NULL)
                 {
                     on_actionSave_As_triggered();
                 }
         
                 else
                 {
                     on_actionSave_triggered();
                 }
                 break;
         
             case QMessageBox::No:
         
                 ui->textEdit->close();
                 exit(0);                                  // that is a brute force exit of your whole program. Most developers do like this.
                 break;
         
             case QMessageBox::Cancel:
         
                 QMessageBox msgBox;        // this is another way, which could be used directly for the main message box as well. Nor requirement to delete later
                 msgBox.exec();                        // you are having another message box here. That is open and waiting. 
                 break;
         
             //default:
                 //break;
             }
         
             this->close();
         }
        
        K Offline
        K Offline
        Kistlak
        wrote on last edited by
        #3

        @koahnig Okay , Thank U very much for that information Bro !!

        Is there a any way to cancel the QMessageBox without exiting the Program ??

        J.HilkJ 1 Reply Last reply
        1
        • K Kistlak

          @koahnig Okay , Thank U very much for that information Bro !!

          Is there a any way to cancel the QMessageBox without exiting the Program ??

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Kistlak yes of course, your program closed because of this line :

          this->close();
          

          this closes the mainwindow and because of its flags that happens to also close your program. The simplest solution would be to exit your function if cancle is clicked:

          I'll hijack @koahnig 's example for this,

          switch(ret)
               {
           
               case QMessageBox::Yes:
           
                   if(TextFile==NULL)
                   {
                       on_actionSave_As_triggered();
                   }
           
                   else
                   {
                       on_actionSave_triggered();
                   }
                   break;
           
               case QMessageBox::No:
           
                   ui->textEdit->close();
                   exit(0);                                  // that is a brute force exit of your whole program. Most developers do like this.
                   break;
           
               default:
               case QMessageBox::Cancel:
                   return;
                  
               }
           
               this->close();
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          K 1 Reply Last reply
          5
          • J.HilkJ J.Hilk

            @Kistlak yes of course, your program closed because of this line :

            this->close();
            

            this closes the mainwindow and because of its flags that happens to also close your program. The simplest solution would be to exit your function if cancle is clicked:

            I'll hijack @koahnig 's example for this,

            switch(ret)
                 {
             
                 case QMessageBox::Yes:
             
                     if(TextFile==NULL)
                     {
                         on_actionSave_As_triggered();
                     }
             
                     else
                     {
                         on_actionSave_triggered();
                     }
                     break;
             
                 case QMessageBox::No:
             
                     ui->textEdit->close();
                     exit(0);                                  // that is a brute force exit of your whole program. Most developers do like this.
                     break;
             
                 default:
                 case QMessageBox::Cancel:
                     return;
                    
                 }
             
                 this->close();
            
            K Offline
            K Offline
            Kistlak
            wrote on last edited by
            #5

            @J.Hilk Thank You very Much Bro !!

            It's Working Perfectly !!

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Calling QApplication::exit would be cleaner.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              4

              • Login

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