title: How to close a dialog with an OK button using a condition
body: <p><img src="https://i.stack.imgur.com/d1pSU.png" alt="My dialog window"></p>
  
  <p><a href="https://pastebin.com/wGT1u9Nc" rel="nofollow noreferrer">My code</a></p>
  
  <p>my first attempt so I could get the result in Mainwindow.cpp but it could be //incorrect</p>
  
  <pre><code>void Dialog::on_buttonBox_accepted()
   {
       Cities.clear();
       for(int row = 0; row&lt;ui-&gt;tableWidget-&gt;rowCount(); row++)
       {
           cities s(get_city(row),get_time(row,3),get_time(row,4));
           Cities.push_back(s);
       }
   }
  // my attempt to make another button but I could not get a result in 
  //Mainwindow.cpp but could check correctly
  void Dialog::on_pushButton_clicked()
  {
      if(cities_is_filled())
      {
          Cities.clear();
          for(int row = 0; row&lt;ui-&gt;tableWidget-&gt;rowCount(); row++)
          {
              cities s(get_city(row),get_time(row,3),get_time(row,4));
              Cities.push_back(s);
          }
      }
  }
  
  //GET RESULT
  void MainWindow::on_actionAdd_train_triggered()
  {
      Dialog e;
      if(e.exec())
      {
         for(auto City: e.Cities)
         {
             ui-&gt;textBrowser-&gt;append(City.city_+ " " + City.depart_+ " " 
             +City.leave_);
         }
      }
  }
  </code></pre>
  
  <p>If the table cell is empty when I press OK, the dialog closes - but I would like it to <em>not</em> close. How can I implement this?</p>