简体   繁体   English

Qt-如何从QListWidget获取选定的自定义项目?

[英]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 我创建了从QListWidget派生的类OptionList和从QListWidgetItem派生的类ListItem

in the class OptionList I tried using QList<ListItem *> items = selectedItems(); 在类OptionList我尝试使用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 ? 我知道selectedItems()函数返回QListWidgetItem的列表,有没有办法将此函数与我从QListWidgetItem派生的ListItem类一起使用?

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. 正确的方法是使用QList<QListWidgetItem *>获取selectedItems()的结果,并在需要从列表中获取项目时使用ListItem *myItem = static_cast<ListItem *>(item)

But, because the list is only of pointers, is safe do this: 但是,由于列表仅是指针,因此可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM