PDA

View Full Version : how to store two dimensional array in a custom table model?



aurora
4th May 2012, 14:01
i got a custom table model in a book.....but i didnt understand how can i set data into such custom model....
For example i have a very big two dimensional array and i want to store that in that custom model so that i can access it quickly....
So please tell me how can i set data into that custom model

The complete code is as shown below...
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtCore>
#include <QtGui>

#include <QMainWindow>



class MulModel : public QAbstractTableModel
{
public:
MulModel( int rows, int columns, QObject *parent = 0 );
Qt::ItemFlags flags( const QModelIndex &index ) const;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const;
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
int columnCount( const QModelIndex &parent = QModelIndex() ) const;



private:
int m_rows, m_columns;
};
#endif // MAINWINDOW_H




mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}



MulModel::MulModel( int rows, int columns, QObject *parent ) :
QAbstractTableModel( parent )
{
m_rows = rows;
m_columns = columns;
}



int MulModel::rowCount( const QModelIndex &parent ) const
{
return m_rows;
}




int MulModel::columnCount( const QModelIndex &parent ) const
{
return m_columns;
}



QVariant MulModel::data( const QModelIndex &index, int role ) const
{
switch( role )
{
case Qt::DisplayRole:
return (index.row()+1) * (index.column()+1);
case Qt::ToolTipRole:
return QString( "%1 x %2" ).arg( index.row()+1 ).arg( index.column()+1 );
default:
return QVariant();
}
}


QVariant MulModel::headerData( int section,
Qt::Orientation orientation, int role ) const
{
if( role != Qt::DisplayRole )
return QVariant();
return section+1;
}



Qt::ItemFlags MulModel::flags( const QModelIndex &index ) const
{
if(!index.isValid())
return Qt::ItemIsEnabled;
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}




main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtCore>
#include <QtGui>
int main( int argc, char **argv )
{
QApplication app( argc, argv );
MulModel model( 12, 12 );
QTableView table;
table.setModel( &model );
table.show();
return app.exec();
}

norobro
4th May 2012, 23:23
Have you looked at the Qt examples? The model (http://doc-snapshot.qt-project.org/4.8/itemviews-addressbook-tablemodel-cpp.html) in the addressbook example uses a different container for data, but is essentially what you need.

ChrisW67
5th May 2012, 01:25
For example i have a very big two dimensional array and i want to store that in that custom model so that i can access it quickly....
So please tell me how can i set data into that custom model

You store the data however you want, it's your model and your data structure. If there's an existing structure containing the data then I'd suggest your work with that.

To make you model writeable you need to return appropriate flags(), implement setData(). If you want the model to be resizable then you need insertRows()/insertColumns(). Unsurprisingly this is in the documentation