Gabriel_Br
Gabriel_Br

Reputation: 119

Qt - How to get selected custom items from a QListWidget?

I created the class OptionList derived from QListWidget and the class ListItem derived from QListWidgetItem

in the class OptionList I tried using QList<ListItem *> items = selectedItems(); to get the selected items from the list, but it shows the following error:

conversion from QList<QListWidgetItem *> to non-scalar type QList<ListItem *> requested

I know that the selectedItems() function returns a list of QListWidgetItem, is there a way to use this function with the ListItem class, which i derived from QListWidgetItem?

Upvotes: 0

Views: 1393

Answers (1)

rflobao
rflobao

Reputation: 572

The correct way is use QList<QListWidgetItem *> to get the result for selectedItems() and use ListItem *myItem = static_cast<ListItem *>(item) when you need to get the item from the list.

But, because the list is only of pointers, is safe do this:

QList<ListItem *> items = *reinterpret_cast<QList<ListItem *>*>(&selectedItems());

Upvotes: 1

Related Questions