PDA

View Full Version : QTableWidget cellwidget selection



tuli
11th October 2013, 08:44
I have a qtablewidget, which i populated with widgets (setCellWidget()). So it doesnt contain any QTableWidgetItems, but only widgets.
I can select the widgets in the table - but how do i get a list of selected cells?

All the IsSelected() stuff operates on QTableWidgetItems, which i dont have.

there is a method to return indexes (QModelIndexList), but it's protected and i cant access it.


how do i do it?

myta212
11th October 2013, 10:31
Hi,
If you want to get selected item in QTableWidget, you can use :

QList<QTableWidgetItem *> QTableWidget::selectedItems ()

Best regards,

Toto

tuli
11th October 2013, 10:43
yes, but i dont have any items, only widgets. So that call always returns an empty list, because no items exists to begin with.

high_flyer
11th October 2013, 11:42
Since QTableWidgetItem is not a QObject, you can try and create a widget which subclasses from QTableWidgetItem:


class WidgetItem : public QWidget, public QTableWidgetItem
{
...
};


Then you can cast your pointer to both QWitdget and QTableWidgetItem allowing you use it both in setCellWidget as well as setItem() allowing you to use the model view part for the selctions and the visible part of cell widget.

I never tried it so no warranty on this ;-)

tuli
11th October 2013, 13:51
clever & sneaky! ;)


I'll give it a shot later on, but it doesnt feel very clean. If someone know an "official" way, please do tell.

high_flyer
11th October 2013, 13:56
What is not clean is the way you are using QTableWidget.
It is an item based widget, and you are not using it with items which is what my solution actually solves.
It wont get much cleaner or more "official" than that - except a proper usage of items.

tuli
11th October 2013, 14:16
is there a better/more official way to use QWidgets in a "table-like" way?

Anyways, you solution works just fine. Thanks!

high_flyer
11th October 2013, 16:10
The question is what exactly you are trying to achieve.
You said very little about the motivation or requirements of what you are doing, so its hard to say.
It sounds almost as what you really need is not a table but a layout.
But I can't really tell based on what you have supplied so far.