Results 1 to 3 of 3

Thread: how to store two dimensional array in a custom table model?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default 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
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include <QtCore>
    4. #include <QtGui>
    5.  
    6. #include <QMainWindow>
    7.  
    8.  
    9.  
    10. class MulModel : public QAbstractTableModel
    11. {
    12. public:
    13. MulModel( int rows, int columns, QObject *parent = 0 );
    14. Qt::ItemFlags flags( const QModelIndex &index ) const;
    15. QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
    16. QVariant headerData( int section, Qt::Orientation orientation,
    17. int role = Qt::DisplayRole ) const;
    18. int rowCount( const QModelIndex &parent = QModelIndex() ) const;
    19. int columnCount( const QModelIndex &parent = QModelIndex() ) const;
    20.  
    21.  
    22.  
    23. private:
    24. int m_rows, m_columns;
    25. };
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 



    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtCore>
    4. #include <QtGui>
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17.  
    18.  
    19. MulModel::MulModel( int rows, int columns, QObject *parent ) :
    20. {
    21. m_rows = rows;
    22. m_columns = columns;
    23. }
    24.  
    25.  
    26.  
    27. int MulModel::rowCount( const QModelIndex &parent ) const
    28. {
    29. return m_rows;
    30. }
    31.  
    32.  
    33.  
    34.  
    35. int MulModel::columnCount( const QModelIndex &parent ) const
    36. {
    37. return m_columns;
    38. }
    39.  
    40.  
    41.  
    42. QVariant MulModel::data( const QModelIndex &index, int role ) const
    43. {
    44. switch( role )
    45. {
    46. case Qt::DisplayRole:
    47. return (index.row()+1) * (index.column()+1);
    48. case Qt::ToolTipRole:
    49. return QString( "%1 x %2" ).arg( index.row()+1 ).arg( index.column()+1 );
    50. default:
    51. return QVariant();
    52. }
    53. }
    54.  
    55.  
    56. QVariant MulModel::headerData( int section,
    57. Qt::Orientation orientation, int role ) const
    58. {
    59. if( role != Qt::DisplayRole )
    60. return QVariant();
    61. return section+1;
    62. }
    63.  
    64.  
    65.  
    66. Qt::ItemFlags MulModel::flags( const QModelIndex &index ) const
    67. {
    68. if(!index.isValid())
    69. return Qt::ItemIsEnabled;
    70. return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    71. }
    To copy to clipboard, switch view to plain text mode 



    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include <QtCore>
    4. #include <QtGui>
    5. int main( int argc, char **argv )
    6. {
    7. QApplication app( argc, argv );
    8. MulModel model( 12, 12 );
    9. QTableView table;
    10. table.setModel( &model );
    11. table.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to store two dimensional array in a custom table model?

    Have you looked at the Qt examples? The model in the addressbook example uses a different container for data, but is essentially what you need.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to store two dimensional array in a custom table 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
    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

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. Replies: 2
    Last Post: 18th December 2011, 23:34
  3. QwtPolar: Plot using a 2-dimensional array
    By hgstoehr in forum Newbie
    Replies: 0
    Last Post: 14th December 2011, 21:39
  4. Multi-dimensional objects array
    By matulik in forum Newbie
    Replies: 3
    Last Post: 23rd October 2011, 02:20
  5. use QVector as 2 dimensional Array
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2010, 12:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.