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



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

  • Moderators

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


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

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



  • @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();
    


  • @J.Hilk Thank You very Much Bro !!

    It's Working Perfectly !!


  • Lifetime Qt Champion

    Hi,

    Calling QApplication::exit would be cleaner.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.