lundi 23 juillet 2018

How to save a QGraphicsItem QList to a file and then read it? Qt c++

I have a list that contains QGraphicsItem. This list allows me to draw lines. I looked in the documentation but I did not see how can we save the points in a file? Then read the points of the file to be able to display them?

Here is my code :

 QList<QGraphicsItem *> graphicsitemList;
 for (int i=0 ; i<graphicsitemList.size(); i++ ){
     for (int j=0 ; j <4; j++){
          this->scene->addLine((float)graphicsitemList[i]->scenePos().x(),
                      (float)graphicsitemList[i]->scenePos().y(),
                      (float)graphicsitemList[indice[j]]->scenePos().x(),
                (float)[indice[j]]->scenePos().y(),pen);

                //I tried this solution but I'm not sure it works.
                  QFile filePoint("points.txt");
        if(filePoint.open(QIODevice::WriteOnly)){
            QDataStream out(&filePoint);
            out << graphicsitemList[i]->scenePos().x();
            out<<',';
            out << graphicsitemList[i]->scenePos().y();
            out<<';';
            out << graphicsitemList[j]->scenePos().x();
            out<<',';
            out << graphicsitemList[j]->scenePos().y();
            out<<';';
            filePoint.close();
      }
  }

Aucun commentaire:

Enregistrer un commentaire