0
votes

I need help creating a function for deleting items from a QListWidget. I need to copy the name of the last item in the list, remove settings from .ini then remove from from QListWidget. So far all I can find is to how to use QList to delete the selected item.

My add function looks like:

void Edge::on_slotNew_pressed()
{
    int i = ui->slotList->count();
    i++;

    QString slotNumber = "Slot" + QString::number(i);
    ui->slotList->addItem(slotNumber);

    QSettings settings("config.ini",QSettings::IniFormat);
    qDebug() << settings.fileName();

    settings.beginGroup(slotNumber);
    settings.setValue("slotSizeW", m_prefs.slotSizeW);
    settings.setValue("slotSizeH", m_prefs.slotSizeH);
    settings.setValue("slotPosX", m_prefs.slotPosX);
    settings.setValue("slotPosY", m_prefs.slotPosY);
    settings.setValue("slotMax", m_prefs.slotMax);
    settings.setValue("slotPriority", m_prefs.slotPriority);
    settings.setValue("slotBorderless", m_prefs.slotBorderless);
    settings.setValue("slotHotkey", m_prefs.slotHotkey);
    settings.endGroup();

    qDebug() << slotNumber;

}

**EDIT:**Updated Remove function
Currently my remove function looks like

void Edge::on_slotDelete_clicked()
{
    QSettings settings("slots.ini",QSettings::IniFormat);

    int i = ui->slotList->count();

    QString slotNumber = "Slot" + QString::number(i);

    QList<QListWidgetItem*> items = ui->slotList->selectedItems();
    foreach(QListWidgetItem* item, items)
    {
        ui->slotList->removeItemWidget(item);
        delete item;
    }

    settings.beginGroup(slotNumber);
    settings.remove("");
    settings.endGroup();

    qDebug() << settings.fileName();
}

I derped and forgot to update the config name so removal works, I just possibly need a better naming convention as it has to be passed as a QString, QListWidgetItem* will not pass correctly

2

2 Answers

0
votes

To delete a group and his content call:

settings.beginGroup(item->getSlotNumber()); // please use here the correct method to get the SlotNumber
settings.remove(""); //removes the group, and all it keys
settings.endGroup();

To get the last name of ui->slotList use the count()-method or the last()-method to get the last item of slotItems:

ui->slotList->item(ui->slotList->count() - 1); // or
ui->slotList->last()
0
votes

The final answer was to use

void Edge::on_slotDelete_clicked()
{
    QSettings settings("slots.ini",QSettings::IniFormat);

    QString slotText;

    QList<QListWidgetItem*> items = ui->slotList->selectedItems();
    foreach(QListWidgetItem* item, items)
    {
        ui->slotList->removeItemWidget(item);
        slotText.append(item->text());//this grabs the name
        delete item;// this deletes list item
    }

    settings.beginGroup(slotText);// only takes QString or const QString argument
    settings.remove("");
    settings.endGroup();

    qDebug() << slotText;
}

And that is how you can delete a selected item and its settings! Not quite what I was looking for but it changes the burden to having a proper naming convention on the save portion. Thank you for all of the support, you pushed me to finding the answer!