Pregunta

When calling QTableWidget::row( const QTableWidgetItem * item ) with a pointer to a valid vertical header item, the function is returning -1. I wouldn't expect this though, since I explicitly set the vertical header item of the row in the table widget to the item pointed to by the pointer by calling QTableWidget::setVerticalHeaderItem ( int row, QTableWidgetItem * item ).

Example code:

qint32 newRow = ui->tableWidget->rowCount();
ui->tableWidget->insertRow( newRow );

QTableWidgetItem *vertHeadItem = new QTableWidgetItem( "Row Header" ); 
ui->tableWidget->setVerticalHeaderItem( newRow, vertHeadItem );

     /* these two outputs print the same address, as expected */
// output is 0xb855b90
qDebug() << vertHeadItem;
// output is 0xb855b90
qDebug() << ui->tableWidget->verticalHeaderItem( newRow );

     /* These two outputs are not the same, but I would expect them to be */
// output is "4"
qDebug() << newRow; // this is the row that contains the verticalHeaderItem
// output is "-1"
qDebug() << ui->tableWidget->row( ui->tableWidget->verticalHeaderItem( newRow ) );

Why does retrieving the row using the verticalHeaderItem address return an invalid row of -1?

¿Fue útil?

Solución

From looking at your code, I'm not sure what you're trying to do makes sense.

In your example, newRow is initialized to the number of rows in your table - which seems to be 4, which is a normal answer.

The row() function on QTableWidget will return the row number the given item appears on. Since you are giving it the vertical header item for your row, it returns -1, because the vertical headers are NOT considered to be in a row -- they are separate from the "content rows" of the table widget. This is the expected behavior.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top