Important: Please read the Qt Code of Conduct -

[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?


  • 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.Text))
    const QByteArray data= stringList.join("\n").toAscii();

  • Thanks alot,

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

    //Multiple selection inside the listwidget and delete selected items
    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)

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

Log in to reply