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



  • 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?


  • Moderators

    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.



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


Log in to reply
 

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