Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved "The program has unexpectedly finished unexpectedly" in qt c++

    General and Desktop
    qtablewidget item search error
    3
    3
    2091
    Loading More Posts
    • 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.
    • L
      Lasith last edited by Lasith

      Following code is used to search the contents of a QTablewidget and display the searched item in the 1st row. The searching is successful but when an item not in the table widget is entered and search button is clicked an error occurs stating the program finished unexpectedly with out displaying the message box in my code!

      void Dialog::on_search_clicked()
      {
      bool found=false;
      QString Line= ui->search->text();

      for(int i=0;i<100;i++){
          if(ui->tableWidget->item(i,0)->text()== Line){
              found = true;
              break;
          }
      }
      if(found){
          ui->tableWidget->clear();
          ui->tableWidget->setItem(0,0,new QTableWidgetItem(Line));
      }
      else{
          QMessageBox::warning(this, tr("Application Name"), tr("The word you are searching does not exist!") );
      }
      

      }

      How can I correct this?

      JonB 1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        I'd encourage you to use standard search methods in Qt's MVC. Like findItems(), or even better: QSortFilterProxyModel.

        Anyway, going back to your code: are you 100% sure that there are 100 items in your tableWidget? I suspect that when you search for non-existing item, the for loop gets to the higher indices and crashes because item(i, 0) tries to access item greater than list size.

        (Z(:^

        1 Reply Last reply Reply Quote 3
        • JonB
          JonB @Lasith last edited by

          @Lasith
          Instead of 100 use ui->tableWidget->rowCount().

          1 Reply Last reply Reply Quote 2
          • First post
            Last post