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
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] write edited qstringlist from qlistwidget to a file

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