How to get the correct row from QTableWidget::item after sortting by column (Qt BUG ?)?

StackOverflow https://stackoverflow.com/questions/13198724

  •  29-07-2021
  •  | 
  •  

Pergunta

I'm doing some table stuff in Qt C++, and today I found something very suspicious after I set sortEnabled(true). I wanted to sort QTableWidget by columns and that's why I set that property to true. Logically I've got what I wanted but I've noticed that when I double click on a row of a table a modal dialog that corresponds to other row is opening. I put in that table QTableWidgetItem-s.

 QTableWidgetItem* widgetItem = new QTableWidgetItem();
 widgetItem->setData(Qt::UserRole, it->id);

On double click signal I'm extracting the row item with QTableWidget::item(int aRow, int aCol), then I'm getting the custom unique ID (at each row I've assigned unique ids).

The point is: after click on column header and sort I'm getting the ID of the row which was on that position before the sorting. The ID that I've got now corresponds to other row because I have sorted by other column.

How I can escape from that problem? Did I do something wrong. Or I have forgotten to set some property to true/false?

I'm thinking that sort affects just the visual side of the table, but not in structural way...

I just want the proper Id, nothing else.

Here Is a short example of the table situation:

Before sorting:

===id=======category==
|   47   |     b     |
|   48   |     a     |
|   49   |     c     |
|   50   |     d     |
======================

When double click on b item(1,1) returns the correct QTableWidget* and ID = 47

Now I'm clicking catetegory header to sort

After Sorting:

===id=======category==
|   48   |     a     |
|   47   |     b     |
|   49   |     c     |
|   50   |     d     |
======================

When double click on a item(1,1) returns the the old one QTableWidget* and ID = 47 AGAIN! I want to get 48 not 47..

Can anyone help me?

Foi útil?

Solução

I don't know why it's not getting you the correct item but you should just listen to QTableWidget::itemDoubleClicked() signal which sends you the actual item so you don't have to go around to get it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top