[SOLVED] howto write a qstringlist from a listwigdet



  • Hi,

    I want to write a qstringlist from data wich is in a listwidget. Idea is that the user selects data in the listwigdet he doesnt want. The resulting qstringlist should than written to a file.

    This is for deleting data out of the widget
    @QListWidgetItem* item = ui->listWidget->takeItem(ui->listWidget->currentRow());
    delete item;@

    How do i now write the content of listwidget in a qstringlist?

    Thanks



  • Iterate all the items, take their text(), append to a QStringList, then write to a file.



  • Hi,

    you have a code snippet for me. i am not that good in programming.



  • @QListWidget * listWidget = ui->listWidget;
    QListWidgetItem * item;
    QStringList stringList;

    // Read all items text and put in a string list
    for(int i = 0; i < listWidget->count(); i++)
    {
    item = listWidget->takeItem(i);
    stringList << item->text();
    }

    // write string list to a file
    QFile file("StringList.txt");
    if(file.open(file.WriteOnly | file.Text))
    {
    const QByteArray data= stringList.join("\n").toAscii();
    file.write(data);
    file.close();
    }@



  • Thanks alot,

    here for all who will have the same problem. This is my code

    @
    //Multiple selection inside the listwidget and delete selected items
    ui->listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
    foreach (QListWidgetItem* item, ui->listWidget->selectedItems())
    delete item;

    //qstringlist of the left items inside the listwidget
    QStringList pickedItem;
    QList<QListWidgetItem*> tmpList = ui->listWidget->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);
    for (int i = 0; i < tmpList.size(); ++i)
    pickedItem.append(tmpList.at(i)->text());

    //write to a file
    QFile newloads("/fs1/PerlQt/QtCreator/test/newloads.inp");
    newloads.open(QFile::WriteOnly|QFile::Truncate|QFile::Text);
    QTextStream out(&newloads);
    out << pickedItem.join("\n");
    newloads.close();@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.