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. How to delete a selected items of txt file displayed in listWidget and then save the changes in txt file?
Forum Updated to NodeBB v4.3 + New Features

How to delete a selected items of txt file displayed in listWidget and then save the changes in txt file?

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 4.0k 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.
  • D Offline
    D Offline
    Dimitrius
    wrote on last edited by
    #1

    I can't delete a selected item in my listWidget and save these changes to the txt file itself.
    Can someone help me by giving an example?
    Below is my code:
    ps: I want to delete the selected rows in the function: void MainWindow :: on_pushButton_3_clicked ().
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFileDialog>
    #include <QMessageBox>
    #include <QFile>
    #include <QTextStream>
    #include <iostream>
    #include <string>
    #include <QListWidget>
    #include <QtGui>
    #include <QStandardItemModel>
    #include <QStandardItem>
    #include <QListWidgetItem>
    #include <qlistwidget.h>
    #include <QModelIndexList>
    #include <QStringListModel>

    QString _filename;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()

    {

    _filename=QFileDialog::getOpenFileName(
                this,
                tr("Open File"),
                "C://",
                "All files (*.*);; Text File &#40;*.txt&#41;"
    
    
                );
    
    
    QMessageBox::information(this, tr("File Name"),_filename);
    
    
    
    if (!_filename.isEmpty()) {
            QFile file&#40;_filename&#41;;
            if (!file.open(QIODevice::ReadOnly)) {
                QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
                return;
                                                 }
    
    
            QTextStream in(&file);
            QString line = in.readLine();
            while (!line.isNull()) {
              new QListWidgetItem(line, ui->listWidget);
              line = in.readLine();
                                   }
    
    
    
    
    
                             }
    

    }

    void MainWindow::on_pushButton_3_clicked()
    {

     QFile file&#40;_filename&#41;;
    
    
     QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
     ?????
    

    }@

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Just delete the items:
      @for(auto i: items) delete i;
      @
      and then iterate over to get each line and write it to file:
      @
      auto items = ui->listWidget->items();
      for(auto i : items) {
      QString s = i->text();
      //write s to file...
      }
      @

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dimitrius
        wrote on last edited by
        #3

        Thanks for the help, but it did not work.
        function:
        @ for(auto i: items) delete i;
        @
        appeared some errors like "i does not name a type" and " 'self' changes meaning in C + + 11; please remove it [-Wc + +0 x-compact]".

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I'm using C++11 syntax, and I highly recommend you to start too. It's a huge time saver. To do that add this line to your .pro file:
          @CONFIG += c++11@
          and re-run qmake. If you don't want to, then you'll have to monkey-type the types yourself:
          @
          //C++11 syntax:
          for(auto i: items) delete i;

          //C++03 syntax:
          int count = items.count();
          for(int i = 0; i < count; ++i) delete items.at(i);
          @
          same goes for the other code.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Well, even without C++/11 you can still do:
            @
            foreach(QListWidgetItem* item, items) {
            QString s = item->text();
            // etc
            }
            @

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I know foreach is nice and well established with Qt but I just have this heavy inner resistance to suggest macro solutions :P Oh well...

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dimitrius
                wrote on last edited by
                #7

                I tried doing this:
                @void MainWindow::on_pushButton_3_clicked()
                {

                 QFile file&#40;_filename&#41;;
                 QTextStream out(&file&#41;;
                 if(!file.open(QFile::WriteOnly | QFile::Text)){
                     qDebug() << "could not open file for writing";
                                 return;
                
                 }
                
                
                 QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
                 for(auto i: items) delete i;
                
                
                 foreach(QListWidgetItem* item, items) {
                     QString s = item->text();
                     out << s ;
                
                
                 }
                
                 file.close();
                
                
                
                 }
                

                @

                But this deletes everything in my text file, or is writing the previously deleted item in the whole file.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Of course, or rather: what happens is undefined.

                  You are accessing the collection items and what was contained in there again after you have deleted all its members. You will need to iterate over the remaining items in the listWidget.

                  Also: it might be a good idea to use a single style of iteration in your code. Now you are mixing C++03 and C++11 styles.

                  1 Reply Last reply
                  0
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    You're using "items" in the second loop, but you just deleted them a line before. Ok, I guess I shouldn't have named them the same but I thought you'll figure that out.

                    So:
                    @
                    auto remainingItems = ui->listWidget->items(); //you're missing that line
                    for(auto item : remainingItems) {
                    QString s = item->text();
                    out << s ;
                    }
                    @

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dimitrius
                      wrote on last edited by
                      #10

                      Ok, I understand the logic but there are two errors in this code the following lines:

                      @auto remainingItems = ui->listWidget->items();@
                      error: no matching function for call to 'QListWidget::items()

                      and:
                      @for(auto item : remainingItems)@
                      error: unable to deduce 'auto&&' from 'remainingItems'

                      1 Reply Last reply
                      0
                      • Chris KawaC Offline
                        Chris KawaC Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        I'm writing from memory. If there's no items() there will be count() and item(row). You can iterate that way.

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Dimitrius
                          wrote on last edited by
                          #12

                          sorry but I didn't understand what did you say.

                          1 Reply Last reply
                          0
                          • Chris KawaC Offline
                            Chris KawaC Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Something like:
                            @
                            auto numRows = ui->listWidget->count();
                            for(int row = 0; row < numRows; ++row) {
                            auto item = ui->listWidget->item(row);
                            QString s = item->text();
                            out << s ;
                            }
                            @

                            I don't mean to be rude but it's taking 2 days to write a couple of lines and you don't seem to try anything yourself. I really can't write "for loops" for you. If you need more assistance please refer to the docs of "QListWidget":http://qt-project.org/doc/qt-5/qlistwidget.html and "QListWidgetItem":http://qt-project.org/doc/qt-5/qlistwidgetitem.html or maybe someone else will find the time. Good luck.

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              Dimitrius
                              wrote on last edited by
                              #14

                              I am new to Qt and so I have many questions and are far from me abusing the goodwill of you.
                              But the posts helped me a lot to understand some points, thanks for the help!

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #15

                                Being new to Qt does not excuse not doing your own studying and experimentation. Qt is a framework build on top of C++. It does require a basic knowledge of C++ in order to be effective developing with it, and in order to be able to understand its structure. I'd say loops are part of the required basic C++ knowledge. However, qt-project.org is not a C++ help channel. I'd recommend you get yourself a good book on C++ (available online, even for free if you want) and get started with the basics first.

                                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