How to store two dimensional array in a custom table model?



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

    @



  • Rofl? Just think.

    | (0)Name | (1)Author | (2)Price


    (0)1 | Name1 | Author1 | Price1


    (N-1)N | NameN | AuthorN | PriceN

    @
    QStandardItemModel *mode = new QStandardItemModel;

    for ( int col = 0; col < 3; ++col )
    {
    QList<QStandartItem*> row;
    for ( int row = 0; row < N; ++row )
    {
    QStandardItem *item = new QStandardItem;
    item->setData("Name#", Qt::DisplayRole);

            row.append(item);
    
            item = new QStandardItem;
            item->setData("Author#", Qt::DisplayRole);
    
            row.append(item);
    
            item = new QStandardItem;
            item->setData("Price#", Qt::DisplayRole);
    
            row.append(item);
       }
    
       model->appendRow(row);
    

    }
    @



  • thank u.....but i tried with this ....i dont know exactly how to start......
    But at the end nothing is displaying in the view....
    Please have a look at this thread...->http://qt-project.org/forums/viewthread/16982/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.