Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Save QMap and QWidgetList



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



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


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



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



  • @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?


Log in to reply