<?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 does QModelIndex know what is my data structure??]]></title><description><![CDATA[<p dir="auto">I am a QT beginner.When I met the QT Model/View for first time,I have some confusion about it.<br />
Just think about the code below(.h file):</p>
<pre><code>class GALLERYCORESHARED_EXPORT AlbumModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum {
        IdRole = Qt::UserRole + 1,
        NameRole
    };
...
private:
    DataBaseManager&amp; mDb;
    std::unique_ptr&lt;std::vector&lt;std::unique_ptr&lt;Album&gt;&gt;&gt; mAlbums;
</code></pre>
<p dir="auto">for me,the data structure is the unique_ptr mAlbums,but now,How does QT Model know what or which is my data strcuture?<br />
I've never use function such like setDataStructureToModel(dsPointer),but they did connect.What did qt do?<br />
And,how do the QModelIndex and my pointer connect?<br />
I want to know what's going on here?</p>
]]></description><link>https://forum.qt.io/topic/97846/how-does-qmodelindex-know-what-is-my-data-structure</link><generator>RSS for Node</generator><lastBuildDate>Sun, 10 May 2026 19:09:21 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/97846.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 20 Dec 2018 15:05:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How does QModelIndex know what is my data structure?? on Sun, 23 Dec 2018 10:44:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/skipper">@<bdi>Skipper</bdi></a> So please close this topic as SOLVED. Thanks!</p>
]]></description><link>https://forum.qt.io/post/501082</link><guid isPermaLink="true">https://forum.qt.io/post/501082</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Sun, 23 Dec 2018 10:44:51 GMT</pubDate></item><item><title><![CDATA[Reply to How does QModelIndex know what is my data structure?? on Fri, 21 Dec 2018 12:40:52 GMT]]></title><description><![CDATA[<p dir="auto">Yes,I've got it finally.The QT Docs is awesome!</p>
]]></description><link>https://forum.qt.io/post/500829</link><guid isPermaLink="true">https://forum.qt.io/post/500829</guid><dc:creator><![CDATA[Skipper]]></dc:creator><pubDate>Fri, 21 Dec 2018 12:40:52 GMT</pubDate></item><item><title><![CDATA[Reply to How does QModelIndex know what is my data structure?? on Thu, 20 Dec 2018 17:27:14 GMT]]></title><description><![CDATA[<p dir="auto">There is one simple example of this ModelView at my <a href="https://github.com/QtDheeru/QtExamples/tree/master/Qt/MyCustomModel" target="_blank" rel="noopener noreferrer nofollow ugc">GIT</a>. You can check &amp; modify &amp; experiment to understand.</p>
]]></description><link>https://forum.qt.io/post/500656</link><guid isPermaLink="true">https://forum.qt.io/post/500656</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Thu, 20 Dec 2018 17:27:14 GMT</pubDate></item><item><title><![CDATA[Reply to How does QModelIndex know what is my data structure?? on Thu, 20 Dec 2018 16:10:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/skipper">@<bdi>Skipper</bdi></a><br />
there is no better document than the source code ;)</p>
<p dir="auto">No i am not aware of an appropriate document, other than the <a href="http://doc.qt.io/qt-5/qabstractitemview.html" target="_blank" rel="noopener noreferrer nofollow ugc">docs</a> and the <a href="http://doc.qt.io/qt-5/examples-itemviews.html" target="_blank" rel="noopener noreferrer nofollow ugc">item-view examples</a> and some good ol' trial and error.</p>
]]></description><link>https://forum.qt.io/post/500649</link><guid isPermaLink="true">https://forum.qt.io/post/500649</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Thu, 20 Dec 2018 16:10:40 GMT</pubDate></item><item><title><![CDATA[Reply to How does QModelIndex know what is my data structure?? on Thu, 20 Dec 2018 16:06:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/raven-worx">@<bdi>raven-worx</bdi></a> I think I got <a href="http://it.So" target="_blank" rel="noopener noreferrer nofollow ugc">it.So</a> can you remcommend me some articals ,books or documents that can give me a deeper understanding or more details on this question? Because I really want to know the inner mechanism about it.</p>
]]></description><link>https://forum.qt.io/post/500648</link><guid isPermaLink="true">https://forum.qt.io/post/500648</guid><dc:creator><![CDATA[Skipper]]></dc:creator><pubDate>Thu, 20 Dec 2018 16:06:55 GMT</pubDate></item><item><title><![CDATA[Reply to How does QModelIndex know what is my data structure?? on Thu, 20 Dec 2018 16:00:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/skipper">@<bdi>Skipper</bdi></a><br />
when you reimplement your model's rowCount() method like that:</p>
<pre><code>int AlbumModel::rowCount(const QModelIndex &amp; parent = QModelIndex()) const
{
      return mAlbums-&gt;count();
}
int AlbumModel::columnCount() const
{
      return ???; // number of properties you want to show of the item
}
</code></pre>
<p dir="auto">Then Qt (the item view) creates a QModelIndex for each cell (row/column) and requests the data with it.</p>
<p dir="auto">If your list items represents the rows and the columns the items's data, you can use the index' row for the list item and the column index for the corresponding data of the item.</p>
]]></description><link>https://forum.qt.io/post/500639</link><guid isPermaLink="true">https://forum.qt.io/post/500639</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Thu, 20 Dec 2018 16:00:49 GMT</pubDate></item><item><title><![CDATA[Reply to How does QModelIndex know what is my data structure?? on Thu, 20 Dec 2018 15:45:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/raven-worx">@<bdi>raven-worx</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/gojir4">@<bdi>Gojir4</bdi></a> Thinks a lot.<br />
But I still have a question.How does the index relationship between QModelIndex and data structure's index be established? i.e why index.row() equals a specifitc i in mAlbums[i]?<br />
code:</p>
<pre><code>QVariant AlbumModel::data(const QModelIndex&amp; index,int role) const {
    if(!isIndexValid(index)) {
        return QVariant();
    }
    const Album&amp; album = *mAlbums-&gt;at((int) index.row());
    switch(role) {
        case IdRole:
            return album.id();;
        case NameRole:
        case Qt::DisplayRole:
            return album.name();
        default:
            return QVariant();
    }
}
</code></pre>
<p dir="auto">I can't understand this line:</p>
<pre><code>    const Album&amp; album = *mAlbums-&gt;at((int) index.row());
</code></pre>
]]></description><link>https://forum.qt.io/post/500637</link><guid isPermaLink="true">https://forum.qt.io/post/500637</guid><dc:creator><![CDATA[Skipper]]></dc:creator><pubDate>Thu, 20 Dec 2018 15:45:56 GMT</pubDate></item><item><title><![CDATA[Reply to How does QModelIndex know what is my data structure?? on Thu, 20 Dec 2018 15:16:31 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/skipper">@<bdi>Skipper</bdi></a>,</p>
<p dir="auto">It doesn't know. You have to implemented at least <code>QVariant QAbstractItemModel::data(const QModelIndex &amp;index, int role = Qt::DisplayRole) const</code> and <code>int QAbstractItemModel::rowCount(const QModelIndex &amp;parent = QModelIndex()) const</code></p>
<p dir="auto">edit: I was too slow :)</p>
]]></description><link>https://forum.qt.io/post/500626</link><guid isPermaLink="true">https://forum.qt.io/post/500626</guid><dc:creator><![CDATA[Gojir4]]></dc:creator><pubDate>Thu, 20 Dec 2018 15:16:31 GMT</pubDate></item><item><title><![CDATA[Reply to How does QModelIndex know what is my data structure?? on Thu, 20 Dec 2018 15:36:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/skipper">@<bdi>Skipper</bdi></a> said in <a href="/post/500624">How does QModelIndex know what is my data structure??</a>:</p>
<blockquote>
<p dir="auto">And,how do the QModelIndex and my pointer connect?</p>
</blockquote>
<p dir="auto">you need to implement the data() method in your model accordingly.<br />
Qt requests data via the data item roles and you need to return the corresponding QVariant data in your data() method for them.</p>
<p dir="auto">Maybe you are looking for <a href="http://doc.qt.io/qt-5/qsqltablemodel.html" target="_blank" rel="noopener noreferrer nofollow ugc">QSqlTableModel</a></p>
]]></description><link>https://forum.qt.io/post/500625</link><guid isPermaLink="true">https://forum.qt.io/post/500625</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Thu, 20 Dec 2018 15:36:30 GMT</pubDate></item></channel></rss>