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



  • 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();
     ?????
    

    }@


  • Moderators

    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...
    }
    @



  • 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]".


  • Moderators

    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.



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


  • Moderators

    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...



  • 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.



  • 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.


  • Moderators

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



  • 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'


  • Moderators

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



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


  • Moderators

    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.



  • 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!



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.