简体   繁体   中英

How do I get text from an item in a qtablewidget?

I want to highlight all of the cells in my table that have the same value in the first column, but have a different value in any other cell. So, for example, if I have two records in my table:

test, 25, 15, 45
test, 25, 5, 45    

I would want to highlight the values 15 and 5.

I have tried the following code, but the text I get from the item calls is always "test" regardless of what item I am accessing.

// Highlight differences in the data
for( int row=0; row < ui->table_Data->rowCount(); row++ )
{
    qDebug() << "going through rows";
    for( int remaining_rows=row+1; remaining_rows < ui->table_Data->rowCount(); remaining_rows++)
    {
        qDebug() << "going through remaining rows";
        for( int column=0; column<ui->table_Data->columnCount(); column++ )
        {
            qDebug() << "going through columns";
            qDebug() << row << ":" << remaining_rows << column;
            qDebug() << ui->table_Data->itemAt(row,column)->text();
            qDebug() << ui->table_Data->itemAt(remaining_rows,column)->text();
            if( ui->table_Data->itemAt(row,column)->text().compare(ui->table_Data->itemAt(remaining_rows,column)->text()) != 0)
            {
                 qDebug() << "data does not match";
                 ui->table_Data->item(row,column)->setBackground(Qt::yellow);
                 ui->table_Data->item(remaining_rows,column)->setBackground(Qt::yellow);
            }
        }
    }
}

It looks like you're using QTableWidget::itemAt when you should be using QTableWidget::item .

Simply put, itemAt finds the QTableWidgetItem at the pixel coordinates (ax, ay), while item returns the QTableWidgetItem at the specified row and column. The text is always "test" because you are always asking the table for the widget very close to (0, 0), which is in the top-left corner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM