Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm finding the terminology you're using a bit awkward, for example MyNestedClass1ViewModel is just a model. I'm not sure what a ViewModel would be.</p> <p>What you're missing in this example is an actual view. MyTreeViewWidget is just a dumb widget that isn't actually a view in Qt terms at all, it's essdentialy just a dumb 'canvas' that you want to display data in. So the way to do this is:</p> <ol> <li><p>You have underlying data in ordinary objects such as NestedDomainModel2. These are not Models in the Qt sense though and I wouldn't name them as such. They're just ordinary objects and don't implement any of the MVC interfaces.</p></li> <li><p>Your MyNestedClass1ViewModel, which is a Qt model class. It accesses the underlying data objects above (1) in the implementation of it's data() and setData() methods.</p></li> <li><p>A view class subclassed from QAbstractItemView. This is what you're missing. It has all the magic hooks to plug into the API of the model class from (2) above. It gets signals from the model telling it when there have been changed, which invoke methods such as dataChanged(), rowsInserted(). You implement these methods to make appropriate changes in your display widget below in point (4).</p></li> <li><p>Your display widget. It doesn't implement any of the model/view API itself and is updated by your view. If it's interactive and can be used to change model data, you do that by calling setData(), insertRows(), removeRows(), etc on the model. The display changes will automatically propagate back to the widget via the view. Be careful not to generate infinite loops of changes propagating from widget->model->view->widget->model->view etc.</p></li> </ol> <p>I have done a similar thing to use a QGraphicsScene/QGraphicsView to display items in a model. Despite it's name QGraphicsView isn't part of the model/view framework, so I implemented a custom view class which drew the model data on the QGraphicsScene.</p> <p>Here's my code, in Python. It draws worlds on a map for an SF wargame:</p> <pre><code>class WorldItemView(QtGui.QAbstractItemView): """ Hidden view which interfaces between the model and the scene. """ def __init__(self, model, parent=None): QtGui.QAbstractItemView.__init__(self, parent) self.hide() self.setModel(model) self.my_model = model self.scene = MapScene(self.my_model) self.resetWorlds() def dataChanged(self, topLeft, bottomRight): top_row = topLeft.row() bottom_row = bottomRight.row() #debug_log("Top row " + str(top_row) + " Bottom row " + str(bottom_row)) for row in range(top_row, (bottom_row + 1)): self.scene.worldChanged(row) def rowsInserted(self, parent, start, end): for row in range(start, (end + 1) ): pmi = self.my_model.getPMI(row) self.scene.insertWorld(pmi) def rowsAboutToBeRemoved(self, parent, start, end): for row in range(start, (end + 1)): self.scene.removeWorld(row) def resetWorlds(self): self.scene.clearWorlds() # Add worlds to scene last_row = self.my_model.rowCount() - 1 self.rowsInserted(None, 0, last_row) </code></pre> <p>I hope that helped.</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.
    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