PDA

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



mortoray
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?

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

mortoray
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.

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

On the top:


#define protected public
#include <QTableWidget>

then:


void DBs::on_table_doubleClicked(const QModelIndex &index)
{
on_table_itemDoubleClicked(ui->table->itemFromIndex(index));
}

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

And by the way,

on_table_itemDoubleClicked(ui->table->item(index.row(),index.column()));

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).

wysota
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();