how to delete a line from QListWidget in which the button was pressed? i know to delete some line (foto) it is necessary to cause method removeItemWidget but for it it is necessary QListWidgetItem but I do not know how to receive it having QWidget which I received from sender ()
The code I use to create strings:
QLayout* l = new QHBoxLayout;
auto le=new QLineEdit;
le->setMaximumWidth(250);
l->addWidget( le );
auto le2=new QLineEdit;
l->addWidget( le2 );
QPushButton* btn = new QPushButton( "Х" );
btn->setObjectName(QString::number(ggg));
connect( btn, SIGNAL( clicked() ), SLOT( close() ) );
btn->setFixedWidth(22);
l->addWidget( btn );
wgt->setLayout( l );
QListWidgetItem* item = new QListWidgetItem( ui->listWidget);
item->setSizeHint( wgt->sizeHint() );
ui->listWidget->setItemWidget(item,wgt);```
CodePudding user response:
removeItemWidget()
only removes the item from the view but it does not delete it so you will have a memory leak. I am not sure if this is what you want, I guess not. But to remove widget from the item, you can delete the widget and it is removed automatically. So the following code should delete the widget and the row as well. This is what I understand from your requirements.
connect( btn, &QPushButton::clicked, [item, wgt]{ delete wgt; delete item; } );
Of course you can only place this line after line
QListWidgetItem* item = new QListWidgetItem( ui->listWidget);
I have not tested it, you may need to use wgt->deleteLater();
instead of delete wgt
.