Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create artificial nodes in QAbstractItemModel for QTreeView
    primarykey
    data
    text
    <p>my question is about Qt and its <a href="http://qt.nokia.com/doc/4.6/qabstractitemmodel.html" rel="nofollow noreferrer">QAbstractItemModel</a>.</p> <p>I have a map of strings and doubles (<code>std::map&lt;stringclass, double&gt;</code>) which I would like to present in a Qt widget. While I could use <a href="http://qt.nokia.com/doc/4.6/qtableview.html" rel="nofollow noreferrer">QTableView</a> for that, I would like to exploit the fact that the keys of the map are of form "<code>abc.def.ghi</code>" where there can be multiple strings that can start with "<code>abc.def</code>" and even more that start with "<code>abc</code>".</p> <p>So I would like to setup a tree data model to present the items in a <a href="http://qt.nokia.com/doc/4.6/qtreeview.html" rel="nofollow noreferrer">QTreeView</a> like</p> <pre><code>(-) abc |--(-)def |--ghi 3.1415 |--jkl 42.0815 |--(+)pqr |--(+)xyz </code></pre> <p>The keys of my <code>std::map</code> are the leaves of the tree, where all other nodes would be temporary and auxillary constructs to support the folding for user convenience. </p> <p>Unfortunately, the methods <code>rowCount</code>, <code>index</code>, <code>columnCount</code>, and <code>data</code> have const-modifiers, so I cannot simply setup a auxillary data structure for the headers inside my <a href="http://qt.nokia.com/doc/4.6/qabstractitemmodel.html" rel="nofollow noreferrer">QAbstractItemModel</a> derivate and change that data structure in there.</p> <p>What would be the best practice for that? Should I setup another class layer between my <code>std::map</code> and the <a href="http://qt.nokia.com/doc/4.6/qabstractitemmodel.html" rel="nofollow noreferrer">QAbstractItemModel</a> or is there a smarter way to do this?</p> <hr> <p>Edit 1: The <code>std::map</code> can change while the <a href="http://qt.nokia.com/doc/4.6/qtreeview.html" rel="nofollow noreferrer">QTreeView</a> is shown and used, so the auxillary nodes might be thrown away and reconstructed. My assumption is that the best way to handle this is to restructure the <a href="http://qt.nokia.com/doc/4.6/qabstractitemmodel.html" rel="nofollow noreferrer">QAbstractItemModel</a> - or should I simply throw that model away and assign a newly constructred one to the <a href="http://qt.nokia.com/doc/4.6/qtreeview.html" rel="nofollow noreferrer">QTreeView</a>? In that case I could set-up all nodes within the constructor without being bothered by the const-ness of the methods, I guess.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload