[英]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. 我想突出显示值15和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 . 当您应该使用QTableWidget :: item时,您似乎正在使用QTableWidget :: itemAt 。
Simply put, itemAt
finds the QTableWidgetItem at the pixel coordinates (ax, ay), while item
returns the QTableWidgetItem at the specified row and column. 简而言之, itemAt
在像素坐标(ax,ay)处找到QTableWidgetItem,而item
在指定的行和列处返回QTableWidgetItem。 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. 文本始终是“ test”,因为您总是向表询问非常接近(0,0)(位于左上角)的小部件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.