Trouble comparing QTableWidgetItems to QStringList items

292 views Asked by At

I build a QStringList from a file:

QFile myTextFile("/home/user/Desktop/file.txt");
QStringList myStringList;
if (!myTextFile.open(QIODevice::ReadOnly))
    {
        QMessageBox::information(0, "Error opening file", myTextFile.errorString());
    }
else
    {  
        while(!myTextFile.atEnd())
        {
            myStringList.append(myTextFile.readLine());
        }
        myTextFile.close();
    }

...the QTableWidget is created and populated, I then want to mark those items in row 5 that match one of the items in myStringList:

int rows = ui->tableWidget->rowCount();
for(int i = 0; i < rows; ++i)
{
    QString str1 = ui->tableWidget->item(i, 5)->text();
    if (myStringList->contains(str1))
    //if(ui->tableWidget->item(i, 5)->text() == "targetstring")
    {
        ui->tableWidget->item(i, 5)->setBackgroundColor(Qt::blue);
    }
}

This compiles, but segfaults on execution. Table exists and is fully populated at this point. If I explicitly pass the targetstring (line commented out) it works fine, but I need to check it against the StringList.

Any suggestions appreciated, been driving me nuts!

1

There are 1 answers

2
UndeadDragon On

You said:

I then want to mark those items in row 5

but in code you set to 5 column:

QString str1 = ui->tableWidget->item(i, 5 <- here)->text();
QTableWidgetItem *  item(int row, int column) const

I think you have only one column, anyway check size of yours table (output table rows and columns count and check it, because your code looks like really unsafe even for prototype).