Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to store two dimensional array in a custom table model?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

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

    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stima_ua
      wrote on last edited by
      #2

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

      }
      @

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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/

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved