Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. "The program has unexpectedly finished unexpectedly" in qt c++

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

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetitemsearcherror
3 Posts 3 Posters 2.4k 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.
  • L Offline
    L Offline
    Lasith
    wrote on last edited by Lasith
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

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

        1 Reply Last reply
        2

        • Login

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