[Qt-interest] 2D Array Abstraction

Alex Strickland sscc at mweb.co.za
Wed Apr 13 10:25:15 CEST 2011


To test a few assumptions I wrote:

#include <QtCore/QList>
#include <QtCore/QDebug>

int main(int argc, char *argv[])
{
     QList< QList< QString > > grid;
     QList< QString > row;
     row << "0,0" << "0,1" << "0,2";
     grid.append( row );
     row.clear();
     row << "1,0" << "1,1" << "1,2";
     grid.append( row );
     row.clear();
     row << "2,0" << "2,1" << "2,2";
     grid.append( row );
     row.clear();

     qDebug() << grid;
     qDebug() << "Remove row 2";
     grid.removeAt( 1 );
     qDebug() << grid;

     qDebug() << "Remove col 2";
     for ( int gridIndex = 0; gridIndex < grid.length(); gridIndex++ ) {
         grid[ gridIndex ].removeAt( 1 );
     }
     qDebug() << grid;
}

Output (slightly edited) is:

(("0,0", "0,1", "0,2") ,
  ("1,0", "1,1", "1,2") ,
  ("2,0", "2,1", "2,2") )
Remove row 2
(("0,0", "0,1", "0,2") ,
  ("2,0", "2,1", "2,2") )
Remove col 2
(("0,0", "0,2") ,
  ("2,0", "2,2") )

1 Can foreach be used somehow to delete columns?
2 Is this all memory safe?
3 Is there already an abstraction out there for this, something with 
addRow, addColumn, insertRow, insertCol, deleteRow, deleteCol etc that 
is not graphically oriented?

Thanks
-- 
Alex



More information about the Qt-interest-old mailing list