View Full Version : QTableWidget::itemFromIndex is protected, why? and a workaround?

18th December 2010, 18:47
I'm writing a delegate to do drawing for cells in a table. The delegate is provided the QModelIndex item and I would like to retrieve the QTableWidgetItem that is associated with it. The problem is that the itemFromIndex member of QTableWidget is protected.

In one case I have a derived table, but in my generic case I use just the standard QTableWidget. Without deriving the class is there another way to get at the QTableWidgetItem from the QModelIndex?

18th December 2010, 18:52
what's about using QTableWidget::item()?

18th December 2010, 19:58
I am using "item( index.row(), index.column() )" right now. I'm just not sure if this is always guaranteed to be the same item as itemFromIndex, or whether it is more/less performant.

I get nervous when something looks like it could be done another way...

Added after 54 minutes:

Okay, looking at the source the itemFromIndex validates the index and then just calls item(row,col). So I should be fine.

26th February 2013, 01:44
The workaround to this stupidity is:

On the top:

#define protected public
#include <QTableWidget>


void DBs::on_table_doubleClicked(const QModelIndex &index)

VOILA! Doing this in production code, by the way! ;] And I really HATE doing this, but....

And by the way,


is also an option, but in my case with a huge database the protected hack did 10 seconds time price from performance point of view (dataset with milliards of records).

26th February 2013, 04:19
And I really HATE doing this, but....
Then don't do this.

I don't know why you need such conversion but if you really want it, just extract the internal pointer from the index.

QTableWidgetItem *item = (QTableWidgetItem*)index.internalPointer();