@Patrick_Li said in Creating add/delete buttons that modify a list:
I haven't been using any code for this, instead I've only been dragging-and-dropping widgets and such onto the UI
After I wrote my comment, I thought so :)
Is there a way to modify the code of the UI at this point?
Unfortunately not.
The Qt Designer is just an auxiliary tool to create simple UI's or help you with designing more complex ones.
Therefore QtDesigner is limited and there are many many things, which are simply not possible to do using the Design Mode only.
You can make direct widget-2-widget connections, when the signal and the slot are compatible, e.g. a connection for clear()'ing the whole list on button click() would be possible to establish just from your UI Designer.
But deleting specific items from that list or adding a new item, wont work this way.
Because I've said, it is easy, I made some tests myself to see if it's really that easy and I came up with this:
Feel free to use it or ask, if something's not clear :)
connect(ui->pushButton_add, &QPushButton::clicked, [this](){
bool ok;
QString text = QInputDialog::getText(this, tr("Create new list item"),
tr("Insert item name:"), QLineEdit::Normal,
"New item", &ok);
if(ok && !text.isEmpty())
ui->listWidget->addItem(text);
});
connect(ui->pushButton_delete, &QPushButton::clicked, [this](){
for(auto item: ui->listWidget->selectedItems()){
int takeAtRow = ui->listWidget->row(item);
auto removedItem = ui->listWidget->takeItem(takeAtRow);
delete removedItem;
}
});