<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to store two dimensional array in a custom table model?]]></title><description><![CDATA[<p dir="auto">i got a custom table model in a book.....but i didnt understand how can i set data into such custom model....<br />
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....<br />
So please tell me how can i set data into that custom model</p>
<p dir="auto">The complete code is as shown below...<br />
mainwindow.h<br />
@    #ifndef MAINWINDOW_H<br />
#define MAINWINDOW_H<br />
#include &lt;QtCore&gt;<br />
#include &lt;QtGui&gt;</p>
<pre><code>#include &lt;QMainWindow&gt;
 
 
 
class MulModel : public QAbstractTableModel
{
public:
MulModel( int rows, int columns, QObject *parent = 0 );
Qt::ItemFlags flags( const QModelIndex &amp;index ) const;
QVariant data( const QModelIndex &amp;index, int role = Qt::DisplayRole ) const;
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const;
int rowCount( const QModelIndex &amp;parent = QModelIndex() ) const;
int columnCount( const QModelIndex &amp;parent = QModelIndex() ) const;
 
 
 
private:
int m_rows, m_columns;
};
#endif // MAINWINDOW_H@
</code></pre>
<p dir="auto">mainwindow.cpp<br />
@    #include "mainwindow.h"<br />
#include "ui_mainwindow.h"<br />
#include &lt;QtCore&gt;<br />
#include &lt;QtGui&gt;<br />
MainWindow::MainWindow(QWidget *parent) :<br />
QMainWindow(parent),<br />
ui(new Ui::MainWindow)<br />
{<br />
ui-&gt;setupUi(this);<br />
}</p>
<pre><code>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 &amp;parent ) const
{
return m_rows;
}
 
 
 
 
int MulModel::columnCount( const QModelIndex &amp;parent ) const
{
return m_columns;
}
 
 
 
QVariant MulModel::data( const QModelIndex &amp;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 &amp;index ) const
{
if(!index.isValid())
return Qt::ItemIsEnabled;
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}@
</code></pre>
<p dir="auto">main.cpp<br />
@<br />
#include &lt;QtGui/QApplication&gt;<br />
#include "mainwindow.h"<br />
#include &lt;QtCore&gt;<br />
#include &lt;QtGui&gt;<br />
int main( int argc, char **argv )<br />
{<br />
QApplication app( argc, argv );<br />
MulModel model( 12, 12 );<br />
QTableView table;<br />
table.setModel( &amp;model );<br />
table.show();<br />
return app.exec();<br />
}</p>
<p dir="auto">@</p>
]]></description><link>https://forum.qt.io/topic/16461/how-to-store-two-dimensional-array-in-a-custom-table-model</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 23:36:41 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/16461.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 04 May 2012 13:05:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to store two dimensional array in a custom table model? on Mon, 07 May 2012 08:05:20 GMT]]></title><description><![CDATA[<p dir="auto">thank u.....but i tried with this ....i dont know exactly how to start......<br />
But at the end nothing is displaying in the view....<br />
Please have a look at this thread...-&gt;<a href="http://qt-project.org/forums/viewthread/16982/" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/forums/viewthread/16982/</a></p>
]]></description><link>https://forum.qt.io/post/138200</link><guid isPermaLink="true">https://forum.qt.io/post/138200</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 07 May 2012 08:05:20 GMT</pubDate></item><item><title><![CDATA[Reply to How to store two dimensional array in a custom table model? on Fri, 04 May 2012 13:25:34 GMT]]></title><description><![CDATA[<p dir="auto">Rofl? Just think.</p>
<h1>| (0)Name  |   (1)Author |  (2)Price</h1>
<hr />
<p dir="auto">(0)1  | Name1     |    Author1  | Price1</p>
<hr />
<p dir="auto">(N-1)N | NameN    |    AuthorN   | PriceN</p>
<p dir="auto">@<br />
QStandardItemModel *mode = new QStandardItemModel;</p>
<p dir="auto">for ( int col = 0; col &lt; 3; ++col )<br />
{<br />
QList&lt;QStandartItem*&gt; row;<br />
for ( int row = 0; row &lt; N; ++row )<br />
{<br />
QStandardItem *item = new QStandardItem;<br />
item-&gt;setData("Name#", Qt::DisplayRole);</p>
<pre><code>        row.append(item);

        item = new QStandardItem;
        item-&gt;setData("Author#", Qt::DisplayRole);

        row.append(item);

        item = new QStandardItem;
        item-&gt;setData("Price#", Qt::DisplayRole);

        row.append(item);
   }

   model-&gt;appendRow(row);
</code></pre>
<p dir="auto">}<br />
@</p>
]]></description><link>https://forum.qt.io/post/138080</link><guid isPermaLink="true">https://forum.qt.io/post/138080</guid><dc:creator><![CDATA[stima_ua]]></dc:creator><pubDate>Fri, 04 May 2012 13:25:34 GMT</pubDate></item></channel></rss>