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] write edited qstringlist from qlistwidget to a file

[SOLVED] write edited qstringlist from qlistwidget to a file

Scheduled Pinned Locked Moved General and Desktop
18 Posts 3 Posters 10.2k 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 All,

    i try to write out a qstringlist to a file, but the file remains empty.

    First put in some data (from a file) to the listWidget.

    @void MainWindow::on_radioButtonLF3_clicked()
    {
    // Loaddefinition
    QFile loadfile("/na/share/LINUX/public/L.txt");
    loadfile.open(QIODevice::ReadOnly|QIODevice::Text);
    QTextStream in(&loadfile);
    QString line = in.readAll();
    QStringList loadlist = line.split(QRegExp("\n"), QString::SkipEmptyParts);
    loadfile.close();
    ui->listWidget->clear();
    ui->listWidget->addItems(loadlist);
    }@

    Now i remove the data from the listWidget (i dont know if this works)

    @void MainWindow::on_removeButton_clicked()
    {

    //ui->listWidget->takeItem(ui->listWidget->currentRow());
    
    QListWidgetItem* item = ui->listWidget->takeItem(ui->listWidget->currentRow());
    delete item;
    

    //ui->listWidget->removeItemWidget(ui->listWidget->currentItem());

    QFile newloads("/fs1/PerlQt/QtCreator/test/new.inp");
    newloads.open(QFile::WriteOnly|QFile::Truncate|QFile::Text);
    QTextStream out(&newloads);
    out << loadlist.join("\n");
    newloads.close();
    

    }@

    Some ideas??

    Thanks in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you have a loadList class member ?
      In that case on_radioButtonLF3_clicked does not update it, it shadows it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        i dont think so. i didnt write that much code up to now, so how do i add such a class?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then you didn't show all code involved in the writing process. Show us the complete method were you write the stringlist

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

            Hi, its there.
            In the first section on_radiobutton...
            I input a file and fill all the stuff into a qstringlist named loadlist.
            The data is shown with a listwidget in the gui.

            The second section is for deleting data from the listwidget.i try here to get rid of selected data from the listwidget by pushing remove_button... so i hope this data is also deleted from the qstringlist loadlist.
            Then open a file for the newloadlist and storing the data from loadlist in newloadlist.the new loadlist should than written to new.inp file.

            Hope that helps. Maybe the problem is that nothing is in loadlist, because loadlist is defined in on_radiobutton and not global. But i thought the everything is managed with the listwigdet

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qxoz
              wrote on last edited by
              #6

              What you trying to do? Write deleted items into file?

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

                No,
                i am trying to write the data from the qstringlist which isnt deleted to a file / qstring.

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  qxoz
                  wrote on last edited by
                  #8

                  In this case better use QListView instead QListWidget. Define loadlist not in on_radioButtonLF3_clicked but as MainWindow member.

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

                    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
                    • Q Offline
                      Q Offline
                      qxoz
                      wrote on last edited by
                      #10

                      Just curios, is there any reason using QListWidget instead QListView? Because your solution of course will work but it little heavy and not efficient.

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

                        The only reason is that my code is copy paste out of the internet and i have no IDEA what i am doing here :)
                        I am an absolute beginner and doing qt for 2 weeks.
                        Could you rewrite my code? This would be nice.

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qxoz
                          wrote on last edited by
                          #12

                          [quote author="mr_maddog" date="1361904066"]Could you rewrite my code? This would be nice.[/quote] :)

                          If this is not a just school task and you need realy learn Qt i advice you start from this book:
                          C++ GUI Programming with Qt 4 - By Jasmin Blanchette, Mark Summerfield
                          this book about Qt 4.1 but it is still best book for beginners.
                          and of course:
                          "http://qt-project.org/doc/":http://qt-project.org/doc/

                          if you have any questions feel free ask here :)

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

                            Hi qxoz,
                            thanks for the reply. I dont have the money for buying a book. I will still trying to finish my project by past and copy. I learned in the past 6 months perl and now qt. my brain is FULL ;)

                            Could u give a hint for changing qwidgdetlist to qlistview.

                            Thanks

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              If you can't afford the book, you still have the examples and demos to go through in Qt's doc (Have look at the MVC part)

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • Q Offline
                                Q Offline
                                qxoz
                                wrote on last edited by
                                #15

                                If just change listwidget with listview and not change the algorithm the code will look like this:
                                @MainWindow::MainWindow(QWidget *parent) :
                                QMainWindow(parent),
                                ui(new Ui::MainWindow)
                                {
                                ui->setupUi(this);
                                stringListModel = new QStringListModel(this);
                                ui->listView->setModel(stringListModel);
                                }@

                                @void MainWindow::on_radioButtonLF3_clicked()
                                {
                                // Loaddefinition
                                QFile loadfile("/na/share/LINUX/public/L.txt");
                                loadfile.open(QIODevice::ReadOnly|QIODevice::Text);
                                QTextStream in(&loadfile);
                                QString line = in.readAll();
                                QStringList loadlist = line.split(QRegExp("\n"), QString::SkipEmptyParts);
                                loadfile.close();
                                stringListModel->setStringList(loadlist);
                                }@

                                @void MainWindow::on_removeButton_clicked()
                                {
                                QModelIndex strIndex = ui->listView->selectionModel()->currentIndex();
                                if(!strIndex.isValid())return;
                                stringListModel->removeRow(strIndex.row());

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

                                }@

                                Here is the very good Qt and C++ tutorials for beginners:
                                "http://voidrealms.com/":http://voidrealms.com/

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

                                  Hi and Thanks qxoz,

                                  and this is now better and faster than with qstringlist?
                                  Why? You rewrote just a few lines (i dont get it....).
                                  Is the qlistview better implemented, works the code faster (because of what?)..

                                  Greetz

                                  1 Reply Last reply
                                  0
                                  • Q Offline
                                    Q Offline
                                    qxoz
                                    wrote on last edited by
                                    #17

                                    Hi mr_maddog!
                                    sorry for the long absence :)
                                    Using model/view gives to your code more flexibility, it is separation of data and view.
                                    And in your case you not need build result string list,
                                    @//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());@
                                    you just get it:
                                    @stringListModel->stringList().join("\n");@

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

                                      Oki doki, thanks

                                      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