How To Clear the textEdit by using a QMessageBox 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 new document button. As usual there are 3 options which are Yes , No & Cancel. Yes and Cancel options okay.

    But , when I click the No button , full application close. But , I want when someone clicks the No button , only clear the textEdit without exiting the program. How can I do this ??

    Here is my code.

    void TextCreator::on_actionNewDocument_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:
    
                      QString TextFile;
                      TextFile = "";
                      ui->textEdit->setText("");
                      break;
    
                  default:
                  case QMessageBox::Cancel:
                      return;
    
                  }
    
             this->close();
    }
    


  • hi,
    this question is very similar to your previous one, and makes me believe, that your statement very new to QT and C++ is very much true.

    Ok, let us break it down a bit.

    everything before your switch/case block and /usually/ everything after the switch/case block is executed and depending on what you clicked in your MessageBox, that part between inside the Switch/case block is executed.

    a break, lets you jump out side the switch/case a return lets you jump out of the whole function.

    So don't forget break, if they are needed.

    Here in your example, again, the line: this->close(); is the problem, that line is always executed when you exit the switch case block via the break command.

    But you actually only want to execute that line in one case, when the user wants to save: so move that line into the case block:

    switch(ret)
                  {
    
                  case QMessageBox::Yes:
    
                      if(TextFile==NULL)
                      {
                          on_actionSave_As_triggered();
                      }
    
                      else
                      {
                          on_actionSave_triggered();
                      }
                      this->close();
                      break;
    
                  case QMessageBox::No:
    
                      QString TextFile;
                      TextFile = "";
                      ui->textEdit->setText("");
                      break;
    
                  default:
                  case QMessageBox::Cancel:
                      return;//could potentially be changed back to break again
    
                  }
    


  • @J.Hilk Okay , now I understood it..

    Again Thank You Very Much !! :D


Log in to reply
 

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