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. Save QMap and QWidgetList
Qt 6.11 is out! See what's new in the release blog

Save QMap and QWidgetList

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 595 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.
  • naaxN Offline
    naaxN Offline
    naax
    wrote on last edited by
    #1

    Hello.

    In my program, I need to save QListWidget. I got this, all works (save and load) but is one problem. Every item on this QListWidget has special double variables and I don't know how I can save it and load.
    After the open program, I want to load QListWidget with these actual variables for items.

    void MainWindow::on_saveButton_clicked()
    {
    
        // save
           QFile file("/Users/Alan/Desktop/QT Projects/Planner/save/MealOne.txt");
           if (file.open(QIODevice::WriteOnly | QIODevice::Text))
           {
               // bind it
               QTextStream stream(&file);
    
               for (int i = 0; i < ui->mealOneLeft->count(); i++)
               {
                   QListWidgetItem *item = ui->mealOneLeft->item(i);
                   stream << item->text() << '\n';
               }
    
               file.close();
           }
    
          
    }
    
    void MainWindow::on_loadButton_clicked()
    {
    
    
        QFile file("/Users/Alan/Desktop/QT Projects/Planner/save/MealOne.txt");
        if (file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            // bind it
            QTextStream in(&file);
    
    
            while(!in.atEnd())
            {
                QString line = in.readLine();
                ui->mealOneLeft->addItem(line);
    
            }
            file.close();
        }
    
    }
    

    How I can save these variables and afterload them back override to QWidgetList items? I'm going in the right way?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Saving in a text file loses the information regarding the original data type, all you can do is guess with trial and error

      void MainWindow::on_loadButton_clicked()
      {
      
      
          QFile file("/Users/Alan/Desktop/QT Projects/Planner/save/MealOne.txt");
          if (file.open(QIODevice::ReadOnly | QIODevice::Text))
          {
              // bind it
              QTextStream in(&file);
      
      
              while(!in.atEnd())
              {
                  const QString line = in.readLine();
                  QListWidgetItem* const newItem = new QListWidgetItem;
                  bool checkConversion = false;
                  newItem->setData(Qt::EditRole,val.toInt(&checkConversion));
                  if (checkConversion){
                      ui->mealOneLeft->addItem(newItem);
                      continue;
                  }
                  newItem->setData(Qt::EditRole,val.toDouble(&checkConversion));
                  if (checkConversion){
                      ui->mealOneLeft->addItem(newItem);
                      continue;
                  }
                  newItem->setData(Qt::EditRole,line);
                  ui->mealOneLeft->addItem(newItem);
              }
              file.close();
          }
      
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      naaxN 1 Reply Last reply
      1
      • VRoninV VRonin

        Saving in a text file loses the information regarding the original data type, all you can do is guess with trial and error

        void MainWindow::on_loadButton_clicked()
        {
        
        
            QFile file("/Users/Alan/Desktop/QT Projects/Planner/save/MealOne.txt");
            if (file.open(QIODevice::ReadOnly | QIODevice::Text))
            {
                // bind it
                QTextStream in(&file);
        
        
                while(!in.atEnd())
                {
                    const QString line = in.readLine();
                    QListWidgetItem* const newItem = new QListWidgetItem;
                    bool checkConversion = false;
                    newItem->setData(Qt::EditRole,val.toInt(&checkConversion));
                    if (checkConversion){
                        ui->mealOneLeft->addItem(newItem);
                        continue;
                    }
                    newItem->setData(Qt::EditRole,val.toDouble(&checkConversion));
                    if (checkConversion){
                        ui->mealOneLeft->addItem(newItem);
                        continue;
                    }
                    newItem->setData(Qt::EditRole,line);
                    ui->mealOneLeft->addItem(newItem);
                }
                file.close();
            }
        
        }
        
        naaxN Offline
        naaxN Offline
        naax
        wrote on last edited by
        #3

        @VRonin

        //Creating new item
               ItemName[ItemID] = ui->addItemName->text();
               ItemKcals[ItemID] = ui->addItemKcal->value();
               ItemProteins[ItemID] = ui->addItemProtein->value();
               ItemFats[ItemID] = ui->addItemFat->value();
               ItemCarbons[ItemID] = ui->addItemCarbon->value();
        
               ui->mealOneLeft->addItem(ui->addItemName->text());
        
        //Adding to map 
        ItemMap.insert(ui->addItemName->text(), ItemID);
        ItemID++;
        

        So is creating a new item. He is added to mealOneLeft (QWidgetList) and name I can save/load, but I don't know what with these variables.

        Pablo J. RoginaP 1 Reply Last reply
        0
        • naaxN naax

          @VRonin

          //Creating new item
                 ItemName[ItemID] = ui->addItemName->text();
                 ItemKcals[ItemID] = ui->addItemKcal->value();
                 ItemProteins[ItemID] = ui->addItemProtein->value();
                 ItemFats[ItemID] = ui->addItemFat->value();
                 ItemCarbons[ItemID] = ui->addItemCarbon->value();
          
                 ui->mealOneLeft->addItem(ui->addItemName->text());
          
          //Adding to map 
          ItemMap.insert(ui->addItemName->text(), ItemID);
          ItemID++;
          

          So is creating a new item. He is added to mealOneLeft (QWidgetList) and name I can save/load, but I don't know what with these variables.

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @naax said in Save QMap and QWidgetList:

          Every item on this QListWidget has special double variables and I don't know how I can save it and load.

          Have you already considered using a database for storing/retrieving your data model?

          Instead of resorting on text or settings files and do custom processing for load/save...

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          naaxN 1 Reply Last reply
          1
          • Pablo J. RoginaP Pablo J. Rogina

            @naax said in Save QMap and QWidgetList:

            Every item on this QListWidget has special double variables and I don't know how I can save it and load.

            Have you already considered using a database for storing/retrieving your data model?

            Instead of resorting on text or settings files and do custom processing for load/save...

            naaxN Offline
            naaxN Offline
            naax
            wrote on last edited by naax
            #5

            @Pablo-J-Rogina

            No, I haven't. It's my the first time where I'm using the load/save functions. Txt. was the first thing. Can you tell me what I should do?

            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