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

        @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 Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #22

        @mpergand
        Just "yuck", and asking for trouble! I'm sure you know this.

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

        @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

        None of this is relevant. And I don't know how a "binary file" would function as a "project file". Anyway, best of luck.

        M S 2 Replies Last reply
        0
        • JonBJ JonB

          @mpergand
          Just "yuck", and asking for trouble! I'm sure you know this.

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

          @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

          None of this is relevant. And I don't know how a "binary file" would function as a "project file". Anyway, best of luck.

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

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

          Just "yuck", and asking for trouble! I'm sure you know this.

          One can think of some kind of zip file

          JonBJ 1 Reply Last reply
          0
          • M mpergand

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

            Just "yuck", and asking for trouble! I'm sure you know this.

            One can think of some kind of zip file

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

            @mpergand
            One can, but I wouldn't recommend it for a newcomer. And it's just unnecessary here when the data format is the OP's own choice and XML/JSON sounds like it would be fine/more suitable. As well as happening to make the "include this data separately from that data" easier. But anyway I think the OP wants to do binary way, that's fine.

            S 1 Reply Last reply
            0
            • JonBJ JonB

              @mpergand
              Just "yuck", and asking for trouble! I'm sure you know this.

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

              @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

              None of this is relevant. And I don't know how a "binary file" would function as a "project file". Anyway, best of luck.

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

              @JonB Correct me if im wrong but programms like photoshop, affinity photo ... all have one single projectfile like "Myfile.ps" that stores all data for the project. As far as i know that is a binary file and i want to build a system similar to that.

              M JonBJ SGaistS 3 Replies Last reply
              0
              • S StudentScripter

                @JonB Correct me if im wrong but programms like photoshop, affinity photo ... all have one single projectfile like "Myfile.ps" that stores all data for the project. As far as i know that is a binary file and i want to build a system similar to that.

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

                @StudentScripter
                As an alternative, you can serialize your buffers as an array of QByteArray.

                1 Reply Last reply
                0
                • S StudentScripter

                  @JonB Correct me if im wrong but programms like photoshop, affinity photo ... all have one single projectfile like "Myfile.ps" that stores all data for the project. As far as i know that is a binary file and i want to build a system similar to that.

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

                  @StudentScripter
                  Each application may save its settings or whatever is whichever format it pleases. I would not know whether your particular apps store in one format or another. If it's your decision to save binary, that's fine.

                  1 Reply Last reply
                  0
                  • S StudentScripter

                    @JonB Correct me if im wrong but programms like photoshop, affinity photo ... all have one single projectfile like "Myfile.ps" that stores all data for the project. As far as i know that is a binary file and i want to build a system similar to that.

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

                    @StudentScripter from what you are writing, it seems that you want to be able to store stuff in a random order and then stream them back again from that random order.

                    Currently there are several formats that are in fact compressed files containing several other files. Take the various office files format for example: one archive containing several files.

                    That said, the simple way to do it is what I wrote: you always store and load your stuff in the same order.

                    The more complex way is what my fellows proposed: one file per element. That way you can stream whatever you want independently.

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

                    M 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @StudentScripter from what you are writing, it seems that you want to be able to store stuff in a random order and then stream them back again from that random order.

                      Currently there are several formats that are in fact compressed files containing several other files. Take the various office files format for example: one archive containing several files.

                      That said, the simple way to do it is what I wrote: you always store and load your stuff in the same order.

                      The more complex way is what my fellows proposed: one file per element. That way you can stream whatever you want independently.

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

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

                      it seems that you want to be able to store stuff in a random order and then stream them back again from that random order.

                      In this case one can use QMap to identify data by its name without any order.
                      I talked about that HERE

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @mpergand
                        One can, but I wouldn't recommend it for a newcomer. And it's just unnecessary here when the data format is the OP's own choice and XML/JSON sounds like it would be fine/more suitable. As well as happening to make the "include this data separately from that data" easier. But anyway I think the OP wants to do binary way, that's fine.

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

                        @JonB Well as i found out may your XML approach seems to be a better pratice as i found out that .docx for example is just a renamed .zip archive with xml files in it.
                        In this case: do you know how to create such an archive containing xml files and how to read from that archive using Qt C++? Haven't found a topic on doing this in a zip like archive.

                        M JonBJ 2 Replies Last reply
                        0
                        • S StudentScripter

                          @JonB Well as i found out may your XML approach seems to be a better pratice as i found out that .docx for example is just a renamed .zip archive with xml files in it.
                          In this case: do you know how to create such an archive containing xml files and how to read from that archive using Qt C++? Haven't found a topic on doing this in a zip like archive.

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

                          @StudentScripter
                          QuaZip
                          KArchive
                          https://forum.qt.io/topic/74306/how-to-manage-zip-file?_=1702397735451&lang=en-GB

                          1 Reply Last reply
                          0
                          • S StudentScripter

                            @JonB Well as i found out may your XML approach seems to be a better pratice as i found out that .docx for example is just a renamed .zip archive with xml files in it.
                            In this case: do you know how to create such an archive containing xml files and how to read from that archive using Qt C++? Haven't found a topic on doing this in a zip like archive.

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

                            @StudentScripter
                            :) Either XML (more feature-rich) or JSON (simpler) could be used for storing information. You still have to decide what to put into it and what to create when reading back out of it. They are just text files with a certain format.

                            Qt offers support for either. Start from https://doc.qt.io/qt-6/qtxml-module.html and the QDomDocument for a full-featured, in-memory XML structure; lower-level XMLStreamWriter/Reader classes, https://doc.qt.io/qt-6/qxmlstreamwriter.html, could be used if you want to write code to just write/read a stream without memory overhead. I suspect you would want the QDocument. Or https://doc.qt.io/qt-6/json.html is where to start from for JSON support.

                            I'm afraid it is still down to you to write the code to decide what to put into the file and how to restore it. But as I said earlier, you won't be able to stream QTreeView or QStandardItemModel via QDataStream because they are both derived from QObject and that cannot be serialized. So you would still have to make the same choices there as to what/how to save/restore.

                            The "zipping like an archive" is just an optional extra at the end. It does not materially affect anything. I suggest you get your serializing sorted out before worrying about that aspect.

                            S 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @StudentScripter
                              :) Either XML (more feature-rich) or JSON (simpler) could be used for storing information. You still have to decide what to put into it and what to create when reading back out of it. They are just text files with a certain format.

                              Qt offers support for either. Start from https://doc.qt.io/qt-6/qtxml-module.html and the QDomDocument for a full-featured, in-memory XML structure; lower-level XMLStreamWriter/Reader classes, https://doc.qt.io/qt-6/qxmlstreamwriter.html, could be used if you want to write code to just write/read a stream without memory overhead. I suspect you would want the QDocument. Or https://doc.qt.io/qt-6/json.html is where to start from for JSON support.

                              I'm afraid it is still down to you to write the code to decide what to put into the file and how to restore it. But as I said earlier, you won't be able to stream QTreeView or QStandardItemModel via QDataStream because they are both derived from QObject and that cannot be serialized. So you would still have to make the same choices there as to what/how to save/restore.

                              The "zipping like an archive" is just an optional extra at the end. It does not materially affect anything. I suggest you get your serializing sorted out before worrying about that aspect.

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

                              @JonB Thank you very much. :) XML is definitely the format i would preferre in that case.
                              And well maybe it's really to worry about zipping the XML files later, but still wanted to know how to create a custom named zip file.
                              Also again thanks for your time an patience with me. I guess it can be hard to read my posts sometimes, but well i guess thats always the case with someone just starting out.

                              EDIT: Also could you may explain the difference between QDomDocument and XMLStreamWriter/Reader? As said i have a GraphicsScene where i want to store data for all my GraphicsItems and a TreeView where i want to store data for all my TreeView items. Im not sure a to use preferrably. So far my code with QDataStream looks like this for the GraphicsItems:

                              
                              //Write to file:
                              QDataStream &operator<<(QDataStream &out, const ResizablePixmapItem &mPixmap)
                              {
                                  //Greift größe und Position ab
                                  int SceneID = mPixmap.getSceneID();
                                  qreal x = mPixmap.selectorFrameBounds().x();
                                  qreal y = mPixmap.selectorFrameBounds().y();
                                  qreal width = mPixmap.selectorFrameBounds().width();
                                  qreal height = mPixmap.selectorFrameBounds().height();
                                  
                                  qreal posX = mPixmap.scenePos().x();
                                  qreal posY = mPixmap.scenePos().y();
                                  
                                  QString ControlName = mPixmap.getControlName();
                                  int FrameWidth = mPixmap.getFrameWidth();
                                  int FrameHeight = mPixmap.getFrameHeight();
                                  
                                  //QPixmap mPix = mPixmap.getPixmap();
                                  
                                  QString ImageFilePath = mPixmap.getImageFilePath();
                                  
                                  
                                  //Speichert die Informationen
                                  out << SceneID << x << y << width << height << posX << posY << ControlName << FrameWidth << FrameHeight << /*mPix <<*/ ImageFilePath;
                                  
                                  return out;
                              }
                              
                              
                              
                              //Read from file:
                              QDataStream &operator>>(QDataStream &in, ResizablePixmapItem &mPixmap)
                              {
                                  int SceneID;
                                  qreal rectX;
                                  qreal rectY;
                                  qreal rectWidth;
                                  qreal rectHeight;
                                  
                                  qreal posX;
                                  qreal posY;
                                  
                                  QString ControlName;
                                  
                                  //QPixmap mPix;
                                  
                                  QString ImageFilePath;
                                  int FrameWidth;
                                  int FrameHeight;
                                  
                                  in >> SceneID >> rectX >> rectY >> rectWidth >> rectHeight >> posX >> posY >> ControlName >> FrameWidth >> FrameHeight  >> /*mPix >>*/ ImageFilePath;
                                  
                                  //SetPixmap muss immer for setSelectorFramebounds kommen, damit die Größe
                                  //beibehalten wird
                                  mPixmap.setSceneID(SceneID);
                                  mPixmap.setPixmap(ImageFilePath);
                                  mPixmap.setSelectorFrameBounds(QRectF(rectX, rectY, rectWidth, rectHeight));
                                  mPixmap.setPos(QPointF(posX, posY));
                                  
                                  mPixmap.setControlName(ControlName);
                                  mPixmap.setImageFilePath(ImageFilePath);
                                  mPixmap.setFrameWidth(FrameWidth);
                                  mPixmap.setFrameHeight(FrameHeight);
                                  
                                  return in;
                              }
                              
                              M 1 Reply Last reply
                              0
                              • S StudentScripter

                                @JonB Thank you very much. :) XML is definitely the format i would preferre in that case.
                                And well maybe it's really to worry about zipping the XML files later, but still wanted to know how to create a custom named zip file.
                                Also again thanks for your time an patience with me. I guess it can be hard to read my posts sometimes, but well i guess thats always the case with someone just starting out.

                                EDIT: Also could you may explain the difference between QDomDocument and XMLStreamWriter/Reader? As said i have a GraphicsScene where i want to store data for all my GraphicsItems and a TreeView where i want to store data for all my TreeView items. Im not sure a to use preferrably. So far my code with QDataStream looks like this for the GraphicsItems:

                                
                                //Write to file:
                                QDataStream &operator<<(QDataStream &out, const ResizablePixmapItem &mPixmap)
                                {
                                    //Greift größe und Position ab
                                    int SceneID = mPixmap.getSceneID();
                                    qreal x = mPixmap.selectorFrameBounds().x();
                                    qreal y = mPixmap.selectorFrameBounds().y();
                                    qreal width = mPixmap.selectorFrameBounds().width();
                                    qreal height = mPixmap.selectorFrameBounds().height();
                                    
                                    qreal posX = mPixmap.scenePos().x();
                                    qreal posY = mPixmap.scenePos().y();
                                    
                                    QString ControlName = mPixmap.getControlName();
                                    int FrameWidth = mPixmap.getFrameWidth();
                                    int FrameHeight = mPixmap.getFrameHeight();
                                    
                                    //QPixmap mPix = mPixmap.getPixmap();
                                    
                                    QString ImageFilePath = mPixmap.getImageFilePath();
                                    
                                    
                                    //Speichert die Informationen
                                    out << SceneID << x << y << width << height << posX << posY << ControlName << FrameWidth << FrameHeight << /*mPix <<*/ ImageFilePath;
                                    
                                    return out;
                                }
                                
                                
                                
                                //Read from file:
                                QDataStream &operator>>(QDataStream &in, ResizablePixmapItem &mPixmap)
                                {
                                    int SceneID;
                                    qreal rectX;
                                    qreal rectY;
                                    qreal rectWidth;
                                    qreal rectHeight;
                                    
                                    qreal posX;
                                    qreal posY;
                                    
                                    QString ControlName;
                                    
                                    //QPixmap mPix;
                                    
                                    QString ImageFilePath;
                                    int FrameWidth;
                                    int FrameHeight;
                                    
                                    in >> SceneID >> rectX >> rectY >> rectWidth >> rectHeight >> posX >> posY >> ControlName >> FrameWidth >> FrameHeight  >> /*mPix >>*/ ImageFilePath;
                                    
                                    //SetPixmap muss immer for setSelectorFramebounds kommen, damit die Größe
                                    //beibehalten wird
                                    mPixmap.setSceneID(SceneID);
                                    mPixmap.setPixmap(ImageFilePath);
                                    mPixmap.setSelectorFrameBounds(QRectF(rectX, rectY, rectWidth, rectHeight));
                                    mPixmap.setPos(QPointF(posX, posY));
                                    
                                    mPixmap.setControlName(ControlName);
                                    mPixmap.setImageFilePath(ImageFilePath);
                                    mPixmap.setFrameWidth(FrameWidth);
                                    mPixmap.setFrameHeight(FrameHeight);
                                    
                                    return in;
                                }
                                
                                M Offline
                                M Offline
                                mpergand
                                wrote on last edited by
                                #34

                                @StudentScripter
                                XML is tedious to use, json easier IMO.

                                S 1 Reply Last reply
                                0
                                • M mpergand

                                  @StudentScripter
                                  XML is tedious to use, json easier IMO.

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

                                  @mpergand Would you say json is more sufficient for my case? As said i may need to save hundreds of GraphicsItems where each Graphicsitem has 50+ different Variables. Also it must be working fine and save on mac, window, linux. (But well i think in this regard their is no big difference between XML and Json.)

                                  SGaistS M JonBJ 3 Replies Last reply
                                  0
                                  • S StudentScripter

                                    @mpergand Would you say json is more sufficient for my case? As said i may need to save hundreds of GraphicsItems where each Graphicsitem has 50+ different Variables. Also it must be working fine and save on mac, window, linux. (But well i think in this regard their is no big difference between XML and Json.)

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

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

                                    @mpergand Would you say json is more sufficient for my case? As said i may need to save hundreds of GraphicsItems where each Graphicsitem has 50+ different Variables. Also it must be working fine and save on mac, window, linux. (But well i think in this regard their is no big difference between XML and Json.)

                                    50+ different variables per items ?
                                    How are you getting the values for them ?
                                    As it is, it sounds like an architectural issue in the making. You are likely trying to store the same data at least twice.

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

                                    1 Reply Last reply
                                    0
                                    • S StudentScripter

                                      @mpergand Would you say json is more sufficient for my case? As said i may need to save hundreds of GraphicsItems where each Graphicsitem has 50+ different Variables. Also it must be working fine and save on mac, window, linux. (But well i think in this regard their is no big difference between XML and Json.)

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

                                      @StudentScripter
                                      I don't know about efficiency.

                                      In XML, you have two kind of parser:
                                      -DOM parser where the xml file is loaded in memory.
                                      -Stream parser where the xml file is read like a file stream (IODevice), more suitable for large files.

                                      Reading an XML document with Qt's DOM classes is a straightforward process. Typically, the contents of a file are supplied to QDomDocument, and nodes are accessed using the functions provided by QDomNode and its subclasses.
                                      The aim is to use the structure provided by QDomDocument by wrapping QDomNode objects in item objects similar to the TreeItem objects used in the Simple Tree Model example.

                                      https://doc.qt.io/qt-6/examples-xml.html

                                      1 Reply Last reply
                                      1
                                      • S StudentScripter

                                        @mpergand Would you say json is more sufficient for my case? As said i may need to save hundreds of GraphicsItems where each Graphicsitem has 50+ different Variables. Also it must be working fine and save on mac, window, linux. (But well i think in this regard their is no big difference between XML and Json.)

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

                                        @StudentScripter
                                        Regardless of which way you serialize (binary, XML, JSON, anything).

                                        You presumably have a QTreeView, bound to a QStandardItemModel. (I guess you have figured how to make that hierarchical.) It is usual to serialize the model but not the view. You find whatever data you have in the model and save/restore that. (And if you can correctly recreate a QStandardItemModel you may not need to worry about saving/restoring recursively.)

                                        You would not normally have anything of the QTreeView to save, why/what would you want with it? What data/settings are in your treeview which are not in your model, which will be restored? Same goes for the "50+ variables", aren't they in your model?

                                        S 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @StudentScripter
                                          Regardless of which way you serialize (binary, XML, JSON, anything).

                                          You presumably have a QTreeView, bound to a QStandardItemModel. (I guess you have figured how to make that hierarchical.) It is usual to serialize the model but not the view. You find whatever data you have in the model and save/restore that. (And if you can correctly recreate a QStandardItemModel you may not need to worry about saving/restoring recursively.)

                                          You would not normally have anything of the QTreeView to save, why/what would you want with it? What data/settings are in your treeview which are not in your model, which will be restored? Same goes for the "50+ variables", aren't they in your model?

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

                                          @JonB The 50+ Variables are stored within my QGraphicsItem Subclass for my GraphicsScene. I want to save the items for my GraphicsScene with their variables as listed above aswell as my QStandardItems for my QTreeView. And yes I subclassed QStandardItemModel, but im building no hierachy there. The hierachy is build by the user using drag and drop.

                                          In shematic: I want to have 2 files XML or Json
                                          -1 file for: All QGraphicsItems with their Variables like Height, Width, Name, ID

                                          int SceneID = mPixmap.getSceneID();
                                              qreal x = mPixmap.selectorFrameBounds().x();
                                              qreal y = mPixmap.selectorFrameBounds().y();
                                              qreal width = mPixmap.selectorFrameBounds().width();
                                              qreal height = mPixmap.selectorFrameBounds().height();
                                              
                                              qreal posX = mPixmap.scenePos().x();
                                              qreal posY = mPixmap.scenePos().y();
                                              
                                              QString ControlName = mPixmap.getControlName();
                                              int FrameWidth = mPixmap.getFrameWidth();
                                              int FrameHeight = mPixmap.getFrameHeight();
                                          

                                          -2n file to save: my QStandardItems/Model. The model looks like this (but i also have an custom StyledItemDelegate, but i guess i don't have to save that)
                                          My full subclassed QStandardItemModel:

                                          #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;
                                          }
                                          
                                          
                                          
                                          
                                          
                                          M 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