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 QTreeView/QStandardItemModel with DataStream to file?
Forum Updated to NodeBB v4.3 + New Features

Save QTreeView/QStandardItemModel with DataStream to file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
40 Posts 5 Posters 6.8k Views 3 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.
  • S StudentScripter

    Hello,
    how could i save and recall the structure of my qtreeview with all it's data to a file and recall it.
    I only have 1 column, but there could be x amount of rows, also every item can have children, so children can also have children.
    I have implemented the EditRole, DecorationRole, CheckStateRole in my delegate.

    Can anybody help me with this? I just want to save the data to a text file and recall it from there.

    #include "ViewLayerList.h"
    #include <QHBoxLayout>
    #include <QCheckBox>
    #include <QLabel>
    #include "ViewLayerDropIndicatorStyle.h"
    #include <QMouseEvent>
    #include "resizablepixmapitem.h"
    #include <QHeaderView>
    #include <QPushButton>
    
    #include <QTimer>
    #include <QApplication>
    #include <QDrag>
    #include <QCursor>
    #include <QMimeData>
    
    #include <QApplication>
    
    ViewLayerList::ViewLayerList(CustomGraphicsScene *scene, QWidget *parent)
        : QTreeView{parent}, scene_durchgereicht(scene)
    {
    
                    
    
    
    
    
    
     setStyle(new ViewLayerDropIndicatorStyle(style()));
    
    
     // Ändern Sie den Abstand zwischen den Items und der vertikalen Scrollbar
        setViewportMargins(0, 0, 50, 0); // Passen Sie den rechten Rand (20) an Ihre Anforderungen an
        
    //Versteckt die sinnlose Kopfzeile
        setHeaderHidden(true);
        setRootIsDecorated(true);
        setMouseTracking(true);
    
    
    
    
    
    
    
    mydelegate = new ViewLayerItemDelegate(this);
    
    
    
    model = new ViewLayerStandartItemModel(0,1,this);
    
    
    
    
    
    
    
    
    this->setModel(model);
    this->setItemDelegate(mydelegate);
    
    
    
    this->setDragDropMode(QAbstractItemView::InternalMove);
    this->setSelectionMode(QAbstractItemView::ExtendedSelection);
    this->setDragEnabled(true);
    this->setAcceptDrops(true);
    this->setDropIndicatorShown(false);
    
    this->setMouseTracking(true);
    
    }
    
    #include "ViewLayerStandartItemModel.h"
    #include "ViewLayerList.h"
    #include <QMimeData>
    #include <QDataStream>
    
    ViewLayerStandartItemModel::ViewLayerStandartItemModel(int rows, int columns, QObject *parent)
        : QStandardItemModel(rows, columns, parent)
    {
    
    }
    
    
    
    
    
    Qt::ItemFlags ViewLayerStandartItemModel::flags(const QModelIndex &index) const {
        Qt::ItemFlags defaultFlags = QStandardItemModel::flags(index);
    
        // Überprüfen Sie, ob der Index zu einem Ihrer speziellen Delegaten gehört
        if (!data(index, CanHaveChildrenRole).toBool()) {
            return (defaultFlags & ~Qt::ItemIsDropEnabled) | Qt::ItemIsDragEnabled; // Entfernen Sie das ItemIsDropEnabled-Flag und fügen Sie das ItemIsDragEnabled-Flag hinzu
        }
        
        
    
    
    
        return defaultFlags | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; // Fügen Sie das ItemIsDragEnabled und ItemIsDropEnabled Flag hinzu
    }
    
    
    
    
    
    
    
    
    Qt::DropActions ViewLayerStandartItemModel::supportedDragActions() const
    {
        return Qt::MoveAction;
    }
    
    
    
    Qt::DropActions ViewLayerStandartItemModel::supportedDropActions() const
    {
        return Qt::MoveAction;
    }
    
    
    
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @StudentScripter
    Not sure quite what you are expecting. You can never serialize either of QTreeView or QStandardItemModel to stream as they derive from QObject. So it's up to you to write code to save whatever you want from these in some format to file and reconstruct on load.

    S 1 Reply Last reply
    1
    • JonBJ JonB

      @StudentScripter
      Not sure quite what you are expecting. You can never serialize either of QTreeView or QStandardItemModel to stream as they derive from QObject. So it's up to you to write code to save whatever you want from these in some format to file and reconstruct on load.

      S Offline
      S Offline
      StudentScripter
      wrote on last edited by
      #3

      @JonB Well than let me ask another question in this case, cause i don't know how:

      How could save all items with childrens (obviously some kind of loop) and reconstruct that?

      M 1 Reply Last reply
      0
      • S StudentScripter

        @JonB Well than let me ask another question in this case, cause i don't know how:

        How could save all items with childrens (obviously some kind of loop) and reconstruct that?

        M Offline
        M Offline
        mpergand
        wrote on last edited by
        #4

        @StudentScripter said in Save QTreeView/QStandardItemModel with DataStream to file?:

        How could save all items with childrens (obviously some kind of loop)

        It's called recursion.
        You need to build a binary tree data structure for that,
        see Simple tree model example

        S 1 Reply Last reply
        1
        • M mpergand

          @StudentScripter said in Save QTreeView/QStandardItemModel with DataStream to file?:

          How could save all items with childrens (obviously some kind of loop)

          It's called recursion.
          You need to build a binary tree data structure for that,
          see Simple tree model example

          S Offline
          S Offline
          StudentScripter
          wrote on last edited by
          #5

          @mpergand Thank you very much. Another problem:

          I already write data to a file using QDatastream, in the same file i want to save the data for my qtreeview. How can i do it that the loading and saving works properly and does not override or read wrong data from the file?

          M 1 Reply Last reply
          0
          • S StudentScripter

            @mpergand Thank you very much. Another problem:

            I already write data to a file using QDatastream, in the same file i want to save the data for my qtreeview. How can i do it that the loading and saving works properly and does not override or read wrong data from the file?

            M Offline
            M Offline
            mpergand
            wrote on last edited by
            #6

            @StudentScripter said in Save QTreeView/QStandardItemModel with DataStream to file?:

            I already write data to a file using QDatastream, in the same file i want to save the data for my qtreeview

            I'm not sure to understand what you mean, sorry.

            S 1 Reply Last reply
            0
            • M mpergand

              @StudentScripter said in Save QTreeView/QStandardItemModel with DataStream to file?:

              I already write data to a file using QDatastream, in the same file i want to save the data for my qtreeview

              I'm not sure to understand what you mean, sorry.

              S Offline
              S Offline
              StudentScripter
              wrote on last edited by
              #7

              @mpergand I have already setup a datastream where i write data from a QGraphicsScene to a file, now i wanted to save the data of the QTreeView in the same file without overriding the data of the QGraphicsScene.
              Also in the reading process I have to make sure somehow that i only read the right data from the file, that is intended for my QTreeView.

              Hope this helps, and thanks for trying to help me out.

              SGaistS 1 Reply Last reply
              0
              • S StudentScripter

                @mpergand I have already setup a datastream where i write data from a QGraphicsScene to a file, now i wanted to save the data of the QTreeView in the same file without overriding the data of the QGraphicsScene.
                Also in the reading process I have to make sure somehow that i only read the right data from the file, that is intended for my QTreeView.

                Hope this helps, and thanks for trying to help me out.

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @StudentScripter hi,

                It is your responsibility to properly setup the sequence when you write and read.

                Basically, you write each widget/model/whatever always in the same order and you do the same when you load from the file.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply
                0
                • SGaistS SGaist

                  @StudentScripter hi,

                  It is your responsibility to properly setup the sequence when you write and read.

                  Basically, you write each widget/model/whatever always in the same order and you do the same when you load from the file.

                  S Offline
                  S Offline
                  StudentScripter
                  wrote on last edited by
                  #9

                  @SGaist But how can i set a sequence? How can let it write all the data of the scene first to the file and than the data of the treeview.

                  How do i than determine till where to read the scene data and where and when to begin reading the QtreeView data?

                  Christian EhrlicherC JonBJ 2 Replies Last reply
                  0
                  • S StudentScripter

                    @SGaist But how can i set a sequence? How can let it write all the data of the scene first to the file and than the data of the treeview.

                    How do i than determine till where to read the scene data and where and when to begin reading the QtreeView data?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @StudentScripter said in Save QTreeView/QStandardItemModel with DataStream to file?:

                    How do i than determine till where to read the scene data and where and when to begin reading the QtreeView data?

                    That's exactly your task you have to find out. It's called 'programming'

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • S StudentScripter

                      @SGaist But how can i set a sequence? How can let it write all the data of the scene first to the file and than the data of the treeview.

                      How do i than determine till where to read the scene data and where and when to begin reading the QtreeView data?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #11

                      @StudentScripter
                      Like we said, it's your duty to write that code. So you know where e.g. the scene data ends so that the treeview data can begin.

                      QDataStream just lets you write (binary) data to a file, i.e. it's concerned with how to write an integer or a string. I would suggest you consider saving your data as either XML or JSON. (They happen to save as text, but that's not really the issue here.) That will allow you more easily to place structure on what you save, so that e.g. you can recognise where the scene data ends and the treeview data begins.

                      S JonBJ 2 Replies Last reply
                      0
                      • JonBJ JonB

                        @StudentScripter
                        Like we said, it's your duty to write that code. So you know where e.g. the scene data ends so that the treeview data can begin.

                        QDataStream just lets you write (binary) data to a file, i.e. it's concerned with how to write an integer or a string. I would suggest you consider saving your data as either XML or JSON. (They happen to save as text, but that's not really the issue here.) That will allow you more easily to place structure on what you save, so that e.g. you can recognise where the scene data ends and the treeview data begins.

                        S Offline
                        S Offline
                        StudentScripter
                        wrote on last edited by
                        #12

                        @JonB @SGaist

                        Well, i understand that my request before sounded wacky, sorry for that. I tried inserting a string as marker to differentiate where which data begins but somehow it wasn't able to read that correctly, it didn't worked. Please give me a hint on how to differentiate between different data in qdatastream files, i want to stick to that format if possible.

                        I have a main save methode from where i trigger the save scene first and the save qtreeview second:

                        //Save Action in der Menuleiste, ermöglicht save in der GraphicsScene
                        void MainWindow::saveActionTriggered()
                        {
                            QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), 
                                                                    "", 
                                                                    tr("MyEditor (*.xle)"));
                                                                    
                            if(!filename.isNull())
                            {
                                scene->saveScene(filename);
                                                    //Saven der ViewLayerList
                                                    ViewLayerList *LayerList = this->findChild<ViewLayerList *>("UNQViewLayerList");
                                                    LayerList->saveTreeView(filename);
                            }                                       
                        }
                        
                        
                        //Load Action in der menuleiste, ermöglicht load in der GraphicsScene
                        void MainWindow::loadActionTriggered()
                        {
                            QMessageBox::StandardButton reply;
                                reply = QMessageBox::warning(this, "Caution", "Loading a file will clear your current project, \ndo you want to save it first?",
                                                            QMessageBox::Yes|QMessageBox::No);                                 
                                if (reply == QMessageBox::No) 
                                {    
                                    QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), 
                                                                                "", 
                                                                                tr("MyEditor (*.xle)")); 
                                    if(!filename.isNull())
                                    {   
                                        scene->loadScene(filename);
                                        
                                                    //Laden der ViewLayerList
                                                    ViewLayerList *LayerList = this->findChild<ViewLayerList *>("UNQViewLayerList");
                                                    LayerList->loadTreeView(filename);
                                    }
                                }
                        }
                        

                        And here ist the load/save methode from my qtreeview:

                            
                        void ViewLayerList::saveTreeView(QString &filename)
                        {
                            qDebug() << "SaveAction TreeView";
                            QFile file(filename);
                            
                            if(!file.open(QIODevice::WriteOnly | QIODevice::Append))
                                return;
                            
                            QDataStream dataStream(&file);
                            int rowCount = model->rowCount();
                            int columnCount = model->columnCount();
                            dataStream << rowCount;
                            dataStream << columnCount;
                         
                            for(int row = 0; row < rowCount; row++)
                                for(int column = 0; column < columnCount; column++) {
                                dataStream << model->item(row, column)->text();
                         
                         
                                }
                            
                            file.close();
                        }
                        
                        
                        
                        
                        
                        void ViewLayerList::loadTreeView(QString &filename)
                        {
                            qDebug() << "LoadAction TreeView";
                            
                            //File laden und öffnen
                            QFile file(filename);
                            if(!file.open(QIODevice::ReadOnly))
                                return;
                        
                            QDataStream dataStream(&file);
                            
                            int rowCount, columnCount;
                            dataStream >> rowCount;
                            dataStream >> columnCount;
                         
                            for(int row = 0; row < rowCount; row++)
                                for(int column = 0; column < columnCount; column++) {
                                QString item;
                                dataStream >> item;
                                QStandardItem * w_item = new QStandardItem(item);
                                model->setItem(row, column, w_item);
                         
                              }
                           
                        
                              
                           
                        
                            file.close();
                        }
                        
                        
                        
                        
                        

                        The code does work on it's own but breaks when second data is saved from my graphicsscene, i guess cause it cannot differentiate.

                        M 1 Reply Last reply
                        0
                        • S StudentScripter

                          @JonB @SGaist

                          Well, i understand that my request before sounded wacky, sorry for that. I tried inserting a string as marker to differentiate where which data begins but somehow it wasn't able to read that correctly, it didn't worked. Please give me a hint on how to differentiate between different data in qdatastream files, i want to stick to that format if possible.

                          I have a main save methode from where i trigger the save scene first and the save qtreeview second:

                          //Save Action in der Menuleiste, ermöglicht save in der GraphicsScene
                          void MainWindow::saveActionTriggered()
                          {
                              QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), 
                                                                      "", 
                                                                      tr("MyEditor (*.xle)"));
                                                                      
                              if(!filename.isNull())
                              {
                                  scene->saveScene(filename);
                                                      //Saven der ViewLayerList
                                                      ViewLayerList *LayerList = this->findChild<ViewLayerList *>("UNQViewLayerList");
                                                      LayerList->saveTreeView(filename);
                              }                                       
                          }
                          
                          
                          //Load Action in der menuleiste, ermöglicht load in der GraphicsScene
                          void MainWindow::loadActionTriggered()
                          {
                              QMessageBox::StandardButton reply;
                                  reply = QMessageBox::warning(this, "Caution", "Loading a file will clear your current project, \ndo you want to save it first?",
                                                              QMessageBox::Yes|QMessageBox::No);                                 
                                  if (reply == QMessageBox::No) 
                                  {    
                                      QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), 
                                                                                  "", 
                                                                                  tr("MyEditor (*.xle)")); 
                                      if(!filename.isNull())
                                      {   
                                          scene->loadScene(filename);
                                          
                                                      //Laden der ViewLayerList
                                                      ViewLayerList *LayerList = this->findChild<ViewLayerList *>("UNQViewLayerList");
                                                      LayerList->loadTreeView(filename);
                                      }
                                  }
                          }
                          

                          And here ist the load/save methode from my qtreeview:

                              
                          void ViewLayerList::saveTreeView(QString &filename)
                          {
                              qDebug() << "SaveAction TreeView";
                              QFile file(filename);
                              
                              if(!file.open(QIODevice::WriteOnly | QIODevice::Append))
                                  return;
                              
                              QDataStream dataStream(&file);
                              int rowCount = model->rowCount();
                              int columnCount = model->columnCount();
                              dataStream << rowCount;
                              dataStream << columnCount;
                           
                              for(int row = 0; row < rowCount; row++)
                                  for(int column = 0; column < columnCount; column++) {
                                  dataStream << model->item(row, column)->text();
                           
                           
                                  }
                              
                              file.close();
                          }
                          
                          
                          
                          
                          
                          void ViewLayerList::loadTreeView(QString &filename)
                          {
                              qDebug() << "LoadAction TreeView";
                              
                              //File laden und öffnen
                              QFile file(filename);
                              if(!file.open(QIODevice::ReadOnly))
                                  return;
                          
                              QDataStream dataStream(&file);
                              
                              int rowCount, columnCount;
                              dataStream >> rowCount;
                              dataStream >> columnCount;
                           
                              for(int row = 0; row < rowCount; row++)
                                  for(int column = 0; column < columnCount; column++) {
                                  QString item;
                                  dataStream >> item;
                                  QStandardItem * w_item = new QStandardItem(item);
                                  model->setItem(row, column, w_item);
                           
                                }
                             
                          
                                
                             
                          
                              file.close();
                          }
                          
                          
                          
                          
                          

                          The code does work on it's own but breaks when second data is saved from my graphicsscene, i guess cause it cannot differentiate.

                          M Offline
                          M Offline
                          mpergand
                          wrote on last edited by mpergand
                          #13

                          @StudentScripter
                          You save your tree as a flat array, then you loose any parent/child relationship.

                          S 1 Reply Last reply
                          0
                          • M mpergand

                            @StudentScripter
                            You save your tree as a flat array, then you loose any parent/child relationship.

                            S Offline
                            S Offline
                            StudentScripter
                            wrote on last edited by
                            #14

                            @mpergand I know i've just done that for now to simpliefy the process as my main problem for now is getting both different data saved and loaded from within the same file.

                            1 Reply Last reply
                            0
                            • JonBJ JonB

                              @StudentScripter
                              Like we said, it's your duty to write that code. So you know where e.g. the scene data ends so that the treeview data can begin.

                              QDataStream just lets you write (binary) data to a file, i.e. it's concerned with how to write an integer or a string. I would suggest you consider saving your data as either XML or JSON. (They happen to save as text, but that's not really the issue here.) That will allow you more easily to place structure on what you save, so that e.g. you can recognise where the scene data ends and the treeview data begins.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #15

                              @StudentScripter

                              @JonB said in Save QTreeView/QStandardItemModel with DataStream to file?:

                              I would suggest you consider saving your data as either XML or JSON.

                              That would encapsulate the tree structure. personally I don't like the idea of a binary data stream with some "markers" you have tried to put in it as "separators". You seem to be only saving string data from your model anyway. Perhaps a touch more initial work, easier to work with in the long run. Don't forget that if you get one thing wrong in binary data it's basically non-recoverable, XML/JSON is easily addressed in a text correction if required.

                              S 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @StudentScripter

                                @JonB said in Save QTreeView/QStandardItemModel with DataStream to file?:

                                I would suggest you consider saving your data as either XML or JSON.

                                That would encapsulate the tree structure. personally I don't like the idea of a binary data stream with some "markers" you have tried to put in it as "separators". You seem to be only saving string data from your model anyway. Perhaps a touch more initial work, easier to work with in the long run. Don't forget that if you get one thing wrong in binary data it's basically non-recoverable, XML/JSON is easily addressed in a text correction if required.

                                S Offline
                                S Offline
                                StudentScripter
                                wrote on last edited by
                                #16

                                @JonB Well but if i have doesn't xml files i can't load/save a projectfile. I want a binary file with all data in it so it functions as a project file. Also i need to save: EditRole, CheckStateRole, DecorationRole, a few UserRoles.

                                M 1 Reply Last reply
                                0
                                • S StudentScripter

                                  @JonB Well but if i have doesn't xml files i can't load/save a projectfile. I want a binary file with all data in it so it functions as a project file. Also i need to save: EditRole, CheckStateRole, DecorationRole, a few UserRoles.

                                  M Offline
                                  M Offline
                                  mpergand
                                  wrote on last edited by
                                  #17

                                  @StudentScripter
                                  The principle to save/load a binary tree is always the same.
                                  Starting with the root item, for each item you need to:

                                  • save its data
                                  • save the nb of children
                                  • save the children items ( looks like we have some recursion here)

                                  For loading, you have to reverse the process:

                                  • create a item
                                  • set its data
                                  • get the nb of children
                                  • create the children ( recursion again)
                                  S 1 Reply Last reply
                                  1
                                  • M mpergand

                                    @StudentScripter
                                    The principle to save/load a binary tree is always the same.
                                    Starting with the root item, for each item you need to:

                                    • save its data
                                    • save the nb of children
                                    • save the children items ( looks like we have some recursion here)

                                    For loading, you have to reverse the process:

                                    • create a item
                                    • set its data
                                    • get the nb of children
                                    • create the children ( recursion again)
                                    S Offline
                                    S Offline
                                    StudentScripter
                                    wrote on last edited by StudentScripter
                                    #18

                                    @mpergand Yes i really appreciate your answer, but that wasn't actually the point of my question yet. I just wanted to know: how do differentiate data within a binary?

                                    How can i determine till where to read the data for my graphicsscene and where to start reading the data for my qtreeview?

                                    Or to formulate it different:
                                    Lets say I add an String named "MARKER" into my datastream and write this string to file. I want the datastream to search for this string and only read data after this string.

                                    M 1 Reply Last reply
                                    0
                                    • S StudentScripter

                                      @mpergand Yes i really appreciate your answer, but that wasn't actually the point of my question yet. I just wanted to know: how do differentiate data within a binary?

                                      How can i determine till where to read the data for my graphicsscene and where to start reading the data for my qtreeview?

                                      Or to formulate it different:
                                      Lets say I add an String named "MARKER" into my datastream and write this string to file. I want the datastream to search for this string and only read data after this string.

                                      M Offline
                                      M Offline
                                      mpergand
                                      wrote on last edited by
                                      #19

                                      @StudentScripter
                                      Your are talking about two different objects, a graphic scene and a treeview and you seems to save their data to the same file, maybe saving them to two separate files may be a solution ?

                                      S 1 Reply Last reply
                                      0
                                      • M mpergand

                                        @StudentScripter
                                        Your are talking about two different objects, a graphic scene and a treeview and you seems to save their data to the same file, maybe saving them to two separate files may be a solution ?

                                        S Offline
                                        S Offline
                                        StudentScripter
                                        wrote on last edited by
                                        #20

                                        @mpergand No it has to be the same file, because i want to able to load from one projectfile. I found that using a qint32 as marker works. First i wanted to use a string but somehow qdatastreams to be unable to find a qstring.

                                        Here is what i've done: (This works)

                                           
                                        void ViewLayerList::saveTreeView(QString &filename)
                                        {  
                                        
                                            qDebug() << "SaveAction TreeView";
                                            QFile file(filename);
                                            
                                            if(!file.open(QIODevice::WriteOnly | QIODevice::Append))
                                                return;
                                            
                                            QDataStream dataStream(&file);
                                        
                                        
                                            dataStream <<  qint32(0xDEADBEEF);  // Write a separator or marker to indicate the end of the first data set
                                        
                                            
                                            
                                            
                                            int rowCount = model->rowCount();
                                        
                                        
                                            dataStream << rowCount;
                                        
                                         
                                            for(int row = 0; row < rowCount; row++)
                                            {
                                                //dataStream << model->item(row, 1)->text();
                                            
                                         
                                            }
                                            
                                        
                                            file.close();
                                        }
                                        
                                        
                                        
                                        
                                        
                                        void ViewLayerList::loadTreeView(QString &filename)
                                        {
                                            qDebug() << "LoadAction TreeView";
                                            
                                            // File laden und öffnen
                                            QFile file(filename);
                                            if (!file.open(QIODevice::ReadOnly))
                                                return;
                                        
                                            QDataStream dataStream(&file);
                                        
                                        
                                            
                                            
                                            while (!dataStream.atEnd()) {
                                                qint32 test;
                                                dataStream >> test;
                                        
                                                  
                                                if (test == qint32(0xDEADBEEF)) {
                                                    qDebug() << "Marker Found";
                                                    break;  // Exit the loop after finding the marker
                                                }
                                            }
                                        
                                            if (dataStream.atEnd()) {
                                                qDebug() << "Error: Marker not found or end of file reached";
                                                file.close();
                                                return;
                                            }
                                        
                                        
                                                // 11. Data in dem File
                                                int rowCount;
                                                dataStream >> rowCount;
                                        
                                                for(int row = 0; row < rowCount; row++) {
                                                    QStandardItem *w_item = new QStandardItem();
                                                    model->setItem(row, 0, w_item);
                                                }
                                            
                                        
                                            file.close();
                                        }
                                        
                                        
                                        
                                        
                                        M 1 Reply Last reply
                                        0
                                        • S StudentScripter

                                          @mpergand No it has to be the same file, because i want to able to load from one projectfile. I found that using a qint32 as marker works. First i wanted to use a string but somehow qdatastreams to be unable to find a qstring.

                                          Here is what i've done: (This works)

                                             
                                          void ViewLayerList::saveTreeView(QString &filename)
                                          {  
                                          
                                              qDebug() << "SaveAction TreeView";
                                              QFile file(filename);
                                              
                                              if(!file.open(QIODevice::WriteOnly | QIODevice::Append))
                                                  return;
                                              
                                              QDataStream dataStream(&file);
                                          
                                          
                                              dataStream <<  qint32(0xDEADBEEF);  // Write a separator or marker to indicate the end of the first data set
                                          
                                              
                                              
                                              
                                              int rowCount = model->rowCount();
                                          
                                          
                                              dataStream << rowCount;
                                          
                                           
                                              for(int row = 0; row < rowCount; row++)
                                              {
                                                  //dataStream << model->item(row, 1)->text();
                                              
                                           
                                              }
                                              
                                          
                                              file.close();
                                          }
                                          
                                          
                                          
                                          
                                          
                                          void ViewLayerList::loadTreeView(QString &filename)
                                          {
                                              qDebug() << "LoadAction TreeView";
                                              
                                              // File laden und öffnen
                                              QFile file(filename);
                                              if (!file.open(QIODevice::ReadOnly))
                                                  return;
                                          
                                              QDataStream dataStream(&file);
                                          
                                          
                                              
                                              
                                              while (!dataStream.atEnd()) {
                                                  qint32 test;
                                                  dataStream >> test;
                                          
                                                    
                                                  if (test == qint32(0xDEADBEEF)) {
                                                      qDebug() << "Marker Found";
                                                      break;  // Exit the loop after finding the marker
                                                  }
                                              }
                                          
                                              if (dataStream.atEnd()) {
                                                  qDebug() << "Error: Marker not found or end of file reached";
                                                  file.close();
                                                  return;
                                              }
                                          
                                          
                                                  // 11. Data in dem File
                                                  int rowCount;
                                                  dataStream >> rowCount;
                                          
                                                  for(int row = 0; row < rowCount; row++) {
                                                      QStandardItem *w_item = new QStandardItem();
                                                      model->setItem(row, 0, w_item);
                                                  }
                                              
                                          
                                              file.close();
                                          }
                                          
                                          
                                          
                                          
                                          M Offline
                                          M Offline
                                          mpergand
                                          wrote on last edited by mpergand
                                          #21

                                          @StudentScripter
                                          You can use a QBuffer instead.
                                          Save your data to two separate buffers, then save them to a file with at the start of the file, the offset of the second buffer where it is located in the file.

                                          JonBJ 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