Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] howto write a qstringlist from a listwigdet
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] howto write a qstringlist from a listwigdet

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mr_maddog
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Santosh Reddy
      wrote on last edited by
      #2

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

      SS

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mr_maddog
        wrote on last edited by
        #3

        Hi,

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Santosh Reddy
          wrote on last edited by
          #4

          @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();
          }@

          SS

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mr_maddog
            wrote on last edited by
            #5

            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();@

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved