title: Qt Model-View programming. QAbstractItemModel and QAbstractListModel. index() and parent()
body: <p>Please help me I'm stuck.</p>
  
  <p>I've got a <code>QList</code> of structures I display in a specific format in a QTreeView. I've actually got not problem with displaying; I've reimplemented <code>data()</code> and it works just well.</p>
  
  <p>My data is "flat", i. e. every node is a root element. Also the data doesn't need to be modified from the view, i. e. it's read-only.</p>
  
  <p>The thing I don't get is what are model indexes. What is the proper use of them.</p>
  
  <p>Currently in <code>data()</code> I return the corresponding item of a <code>QList</code> by the model <em>row</em> (<code>index.row()</code>). It seems to be legit, and that's what <code>data()</code> is for.</p>
  
  <p>Apart of <code>data()</code> I've reimplemented <code>rowCount()</code>, <code>columnCount()</code>, <code>flags()</code> and <code>headerData()</code>.</p>
  
  <p>It is enough for a <code>QAbstractListModel</code>, but to subclass from <code>QAbstractItemModel</code>, I need also to reimplement <code>parent()</code> and <code>index()</code>. And I have no idea what exactly I need to do in these functions. If I return an empty model index (<code>return QModelIndex()</code>), I get no data displayed.</p>
  
  <p>I understand the idea of MVC: the view is able to show the data in many ways that are most appropriate and useful. For example, the view may sort the data alphabetically, or anything, so the order of the rows displayed will <em>differ</em> from the way the data is organized in the application's internals. But I don't understand how would that work in my case, the case of my application and Qt.</p>
  
  <p>The view just simply queries my model's <code>data()</code> function for each row and each column, and because the data I have doesn't get modified, e. g. sorted, the view always displays the data in the same order (even if I click the header to sort the view). I can sort the list myself, but then I will lose the right order (for example, the app wants to get an item <code>#3</code>, but after the list had been sorted, the needed item has got another number), and that doesn't look like MVC at all: when the View of the Data changes, the Data changes too. It shouldn't.</p>
  
  <p>The above was the question. Here comes code.</p>
  
  <p>Well, I thought there isn't much need for a code to illustrate my question as I already wrote enough words. I've actually got two problems: I'm currently subclassed from <code>QAbstractListModel</code>, but I want to subclass from <code>QAbstractItemModel</code>. So I need to write <code>index()</code> and <code>parent()</code>. Also I don't understand how sorting should work.</p>
  
  <p>Anyway I've suddenly googled <a href="http://www.java2s.com/Code/Cpp/Qt/stringlistmodelexample.htm" rel="nofollow noreferrer">this page</a>. It looks <em>very</em> close to what do I have (apart of editing: I don't need to edit my data with the view, so I didn't implement the corresponding functions; I also display pictures, not text only (no problem)). I would like to know how can I do the same (look at that page) with <code>QAbstractItemModel</code>.</p>
  
  <p>If I'm unclear please let me know. :/</p>