Failed to recover a custom item in a Qlistwidget

Asked

Viewed 60 times

0

I am trying to recover data from a Qlistwidgetitem when I click on the list but am not getting it. I made a custom Widget to list the elements, but although I can not see in the event click recover my element.

Event click of the application

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 the 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);
    }
}

1 answer

-1

The cast is being done wrong and in the wrong place to retrieve this information if you use itemWidget, it will return the widget that was created.

Changing where you have:

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

To:

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

It is possible to recover the content and use normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.