0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Don Angelo Annoni
  • 350
  • 1
  • 2
  • 17

1 Answers1

1

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.

Stephen Chu
  • 12,724
  • 1
  • 35
  • 46
  • I'm actually listening to `void cellDoubleClicked (int row, int column)`. I'm gonna try your suggestion and when I see the result I will mark the answer. Thanks :) Btw.. do you have any idea why with my method (listening to `cellDoubleClicked`) doesn't work? Because It will be great to get the task done with minimal change of the code logic... If there is no other way - I'm gonna use the rewritten method which you suggested me. – Don Angelo Annoni Nov 02 '12 at 18:00