K
K
kamasit2021-10-19 21:10:47
Qt
kamasit, 2021-10-19 21:10:47

How to re-add a custom widget?

Column::Column(QWidget *parent)
      : QListWidget(parent)
{
    Button *button = new Button(this);
    {
        connect(button, &Button::clicked,
                this, &Column::slot_addCard);
    }

    QListWidgetItem *buttonItem = new QListWidgetItem(this);
    buttonItem->setSizeHint(button->sizeHint()); // change size
    addItem(buttonItem);
    setItemWidget(buttonItem, button);

    qDebug() << buttonItem;
    qDebug() << button;
}

void Column::slot_addCard()
{
    const int COUNT = this->count() - 1;
    Button *button = dynamic_cast<Button*>(itemWidget(item(COUNT)));
    QListWidgetItem *buttonItem = this->takeItem(COUNT);

    qDebug() << buttonItem;
    qDebug() << button;

    Card *card = new Card(this);
    QListWidgetItem *cardItem = new QListWidgetItem(this);
    cardItem->setSizeHint(card->sizeHint());
    setItemWidget(cardItem, card);

    addItem(buttonItem);
    setItemWidget(buttonItem, button);  // Segmentation fault !!!
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacob E, 2021-10-19
@Zifix

Maybe setItemWidget() on a QListWidgetItem object can only be called once? What happens if there is no last line? Reversal just to the fields button fulfills without falling?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question