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 d i load a textfile to QListwidget and also save it back

How d i load a textfile to QListwidget and also save it back

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlistwidgetqfilec++
5 Posts 3 Posters 2.2k Views
  • 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.
  • L Offline
    L Offline
    learnist
    wrote on 14 Jun 2020, 21:26 last edited by
    #1

    How do i load a text file into QListWidget , such that each line in the textfile is an item in QListWidget,

    and once i populate it, i also want to save all items of listwidget into a text file , such that each item of listwidget is in a line in the text file , to accomplish that i am attempting this

    QString temp;
      for(int i=0; i< ui->mylistwidget->count();i++)
    {
    QListWidgetItem* item = ui->mylistwidget->item(i);
    temp += item->text();
    }
    std::string total_script = temp.toStdString();
    

    i got all the text of listwidget items in a string , what do i do next if i want to save it in a text file? and also how do i load text file and populate my qlistwidget with items ?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 14 Jun 2020, 21:49 last edited by
      #2

      Hi and welcome to the forums.

      You could do like this using QFile and TextStream

          // save
          QFile file("e:/test.txt");
          if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
              // bind it
              QTextStream stream(&file);
              for (int i = 0; i < ui->mylistwidget->count(); i++) {
                  QListWidgetItem *item = ui->mylistwidget->item(i);
                  stream << item->text() << '\n';
              }
      
              file.close();
          }
      
           ui->mylistwidget->clear();
          // load
          {
      
              QFile file("e:/test.txt");
              if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                  // bind it
                  QTextStream in(&file);
      
                  while(!in.atEnd()) {
                      QString line = in.readLine();
                      ui->mylistwidget->addItem(line);
                  }
      
                  file.close();
              }
      
      
          }
      
      L 2 Replies Last reply 14 Jun 2020, 22:06
      8
      • M mrjj
        14 Jun 2020, 21:49

        Hi and welcome to the forums.

        You could do like this using QFile and TextStream

            // save
            QFile file("e:/test.txt");
            if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                // bind it
                QTextStream stream(&file);
                for (int i = 0; i < ui->mylistwidget->count(); i++) {
                    QListWidgetItem *item = ui->mylistwidget->item(i);
                    stream << item->text() << '\n';
                }
        
                file.close();
            }
        
             ui->mylistwidget->clear();
            // load
            {
        
                QFile file("e:/test.txt");
                if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                    // bind it
                    QTextStream in(&file);
        
                    while(!in.atEnd()) {
                        QString line = in.readLine();
                        ui->mylistwidget->addItem(line);
                    }
        
                    file.close();
                }
        
        
            }
        
        L Offline
        L Offline
        learnist
        wrote on 14 Jun 2020, 22:06 last edited by
        #3

        @mrjj
        Thank you so much, can i use the file dialog box in this, if so how ?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mark Durkot
          wrote on 15 Jun 2020, 01:08 last edited by Mark Durkot
          #4

          Yes sure, use class QFileDialog, here is the documentation to it, so you can see all the functions and what they do. I would also really recommend using qt documentation, its a cool thing:)
          QFileDialog: https://doc.qt.io/qt-5/qfiledialog.html

          QString fileName = QFileDialog::getOpenFileName(this, "Please, chose a file to open");
          
          QFile file(fileName); // this creates a QFile object of our selected file
          

          This code gets you the name of the file you have chosen in the file dialog.

          Now if you want to save the file also via file dialog, then call:

          QString fileName = QFileDialog::getSaveFileName(this, "Please, chose where to save the file");
          
          QFile file(fileName); // this creates a QFile object of our selected file
          

          "This is a convenience static function that will return a file name selected by the user. The file does not have to exist." as the qt documentation says.

          Hope it will help)
          Have fun programming ;)

          1 Reply Last reply
          7
          • M mrjj
            14 Jun 2020, 21:49

            Hi and welcome to the forums.

            You could do like this using QFile and TextStream

                // save
                QFile file("e:/test.txt");
                if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                    // bind it
                    QTextStream stream(&file);
                    for (int i = 0; i < ui->mylistwidget->count(); i++) {
                        QListWidgetItem *item = ui->mylistwidget->item(i);
                        stream << item->text() << '\n';
                    }
            
                    file.close();
                }
            
                 ui->mylistwidget->clear();
                // load
                {
            
                    QFile file("e:/test.txt");
                    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                        // bind it
                        QTextStream in(&file);
            
                        while(!in.atEnd()) {
                            QString line = in.readLine();
                            ui->mylistwidget->addItem(line);
                        }
            
                        file.close();
                    }
            
            
                }
            
            L Offline
            L Offline
            learnist
            wrote on 15 Jun 2020, 11:17 last edited by
            #5

            @mrjj Thanks so much , really helped me understand.

            1 Reply Last reply
            0

            3/5

            14 Jun 2020, 22:06

            • Login

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