Failed to retrieve a custom item in a QListWidget

0

I'm trying to retrieve data from a QListWidgetItem when I click on the list, but I'm not getting it. I made a custom Widget to list the elements, however although I can not visualize I can not in the click event retrieve my element.

Application click event

void Start::on_phonebookList_itemClicked(QListWidgetItem *item)
{

    // ContactListItem *cItem = (ContactListItem*)item;
    ContactListItem * cItem = (ContactListItem * )ui->contactPhonebookNumberList->itemWidget(item);
    this->contactSelected = cItem->getContact();
    ui->contactPhonebookName->setText( this->contactSelected->name.toLatin1().data());
    const QVector<PhonebookNumber *> list = this->contactSelected->getNumbers();
    for(int i=0;i!=list.size();i++){
        PhonebookNumber * pn =list[i];
        ui->contactPhonebookNumberList->addItem(pn->number.toLatin1().data());
    }
    ui->nav->setCurrentIndex(NavigationTabs::CONTACT);
}

Popular to list

void Start::on_Start_onReloadPhonebookListEvent(const PhonebookType & type)
{
    if(this->getCurrentPhonebookType() != type){
        return;
    }
    const QVector <PhonebookContact*> list = Phonebook::getInstance()->getPhoobook(type);

    this->ui->phonebookList->clear();
    for(int i = 0; i!= list.size();i++){
        QListWidgetItem *widgetItem = new QListWidgetItem();
        this->ui->phonebookList->addItem(widgetItem);
        ContactListItem * contactItem  = new ContactListItem();
        contactItem->setContact(list[i]);
        widgetItem->setSizeHint(QSize(contactItem->width(),contactItem->height()));
        this->ui->phonebookList->setItemWidget(widgetItem,contactItem);
    }
}
    
asked by anonymous 31.08.2018 / 19:02

1 answer

-1

Cast is being done wrong and in the wrong place, to retrieve this information itemWidget, it will return the widget that was created.

Changing where you have it:

ContactListItem * cItem = (ContactListItem * )ui->contactPhonebookNumberList->itemWidget(item);

To:

  ContactListItem *cItem = qobject_cast <ContactListItem *>(item->listWidget()->itemWidget(item));

You can retrieve the content and use it normally.

    
31.08.2018 / 22:00