Creating add/delete buttons that modify a list
-
I am currently creating a GUI with several lists (Radiologist ID, Presets, Nodules).
I am trying to create add/remove buttons for each list that add a new item to the list, and remove a selected item from the list, respectively.
After searching for some time, I haven't been able to find any way to do this. -
Hi and welcome to devnet,
Which list are you referring to ?
You seem to already have add and delete buttons for the three top elements which looks good.
Are you talking about the annotations part ?
-
That's normal, you have to code their functionality.
-
@Patrick_Li said in Creating add/delete buttons that modify a list:
After searching for some time, I haven't been able to find any way to do this.
Dont know about your coding background, but it's not that hard.
Connect the add button to
either with lambda or write some code around it, to make it work (dialog or whatever prompt to enter new item text). Or even leave the text empty and edit after the item has been added.
Connect the delete button to
and pick one or all items from
to remove them from your list.
Note, that you have to delete your
QListWidgetItem
manually after you have taken it from your list. -
@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 forclear()
'ing the whole list on buttonclick()
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; } });