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 Clear the textEdit by using a QMessageBox in Qt ??
Forum Update on Monday, May 27th 2025

How To Clear the textEdit by using a QMessageBox in Qt ??

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 1.3k 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
    #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 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();
    }
    
    1 Reply Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      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
      
                    }
      

      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
      2
      • J.HilkJ J.Hilk

        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
        
                      }
        
        K Offline
        K Offline
        Kistlak
        wrote on last edited by
        #3

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

        Again Thank You Very Much !! :D

        1 Reply Last reply
        0

        • Login

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