2016-11-18 6 views
1

私は数字を "描く"ことができるボードを持っています。QTファイルに保存する方法QPoint 2dアレイ

enter image description here

これは、ボードのコード

私の質問は、私が選択したのQPointからNUMERをファイルに保存することができますどのようにリストに更新ポイント

QVector<QPoint> points; 
    void PrintRectangle::updateIndexFromPoint(const QPoint &point) 
    { 
     int x = point.x() - 20; 
     int y = point.y() - 20; 
     bool removed = false; 

     if(((x >= 0) && (x <= 300)) && ((y >= 0) && (y <= 300))) 
     { 
      mXIndex = x/60; //rec width + spacing 
      mYIndex = y/60; //rec height + spacing 

      for(int k=0; k<points.size(); k++){ 

       qDebug("%d %d", points[k].x(), points[k].y()); 

       if(points[k].x() == mXIndex && points[k].y() == mYIndex){ 

        points.remove(k); 
        removed = true; 
       } 

      } 

      if(!removed){ 
       points.append(QPoint(mXIndex,mYIndex)); 
      } 
     } 
    } 

void PrintRectangle::paintEvent(QPaintEvent *) 
{ 
for(int i=0; i<5; i++) 
    { 
     ypos=20; 
     for(int j=0; j<5; j++) 
     { 
      QColor color = Qt::white; 
      for(int k=0; k<points.size(); k++){ 

        if(i == points[k].x() && j == points[k].y()) 
        { 
        color = Qt::black; 
        } 

      } 
      p.fillRect(xpos,ypos,recWidth,recHeight,color); 
      ypos+=60; 
     } 
     xpos+=60; 
    } 
} 

と次の関数であり、矩形。

例えば、ファイル内のNUMER 1

0 0 1 0 0 
0 1 1 0 0 
1 0 1 0 0 
0 0 1 0 0 
0 0 1 0 0 
+0

'のstd :: fstream'? –

+0

私は '0'と' 1'をどのように認識するべきか、 'QPoint'を反復しますか? – lukassz

+0

あなたはそれを描くことができます。もちろん、あなたは '0'と' 1'を知っていますか? –

答えて

1

は単にファイルにあなたのポイントを保存するためにQDataStreamを使用します。

void savePoints(QVector<QPoint> points) 
{ 
    QFile file("points.bin"); 
    if(file.open(QIODevice::WriteOnly)) 
    { 
     QDataStream out(&file); 
     out.setVersion(QDataStream::Qt_4_0); 
     out << points; 
     file.close(); 
    } 
} 

QVector<QPoint> loadPoints() 
{ 
    QVector<QPoint> points; 
    QFile file("points.bin"); 
    if(file.open(QIODevice::ReadOnly)) 
    { 
     QDataStream in(&file); 
     in.setVersion(QDataStream::Qt_4_0); 
     in >> points; 
     file.close(); 
    } 
    return points; 
} 
関連する問題