2016-11-18 70 views
1

我有我可以「畫」一個數字的棋盤。QT如何保存到文件QPoint 2d陣列

enter image description here

這是名單板

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

而接下來的功能,有哪些更新的點的代碼

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

我的問題是我怎麼能保存到文件NUMER從QPoint選擇長方形。

例如。在文件

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

我但是如何識別'Q''迭代'0'和'1'? – lukassz

+0

你可以畫出來,當然你知道哪個是'0'和'1',不是嗎? –

回答

1

NUMER 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; 
}