hosscomp
hosscomp

Reputation: 21

How to use QAbstractItemModel?

I need to use QAbstractItemModel with QTreeView in PyQt.

In the dropMimeData method I want to remove the source row (if it is a certain MIME_TYPE.) How do I get the row and parentIndex of the source? Or do I need to do that from some other method.

def dropMimeData(self, mimedata, action, row, column, parentIndex):

Upvotes: 2

Views: 3356

Answers (1)

jdi
jdi

Reputation: 92647

My comments were running too long so I figured I would start an answer now with the info.

QAbstractItemModel is a base class which is there for when you need a customized way of modeling your data that does not fit one of the existing ones, such as where to source the data and how to represent them as items. It is not ready to use out of the box, as it needs to have a number of methods implemented. You should probably not be starting with this class unless you have a compelling reason to do so, as it requires much more work to get up and running.

For simply needing to do drag and drop, and using basic items, a QStandardItemModel should be ready to go for your use. You just create QStandardItems and populate the model. For drag and drop, you would subclass the model and just implement the appropriate drag*Event and drop*Event methods to suit your needs.

Using a QStandardItemModel + QTreeView allows you to have multiple views all using the same model and visualizing it different at the same time. But given that you said you want to keep two different sets of independent data, and that you are new to Qt, I would highly recommend you just use two QTreeWidgets. A QTreeWidget is an all-inclusive package of the view and the model. This will be much easier for you to use for now.

Using the QTreeWidget, you would subclass them and implement the necessary drag and drop events just as you would for QTreeView, but you no longer have to worry about the models separately.

Here is a link to a post regarding drag and drop with QTreeWidget: http://www.qtcentre.org/threads/5910-QTreeWidget-Drag-and-drop
They mention the recommended methods to implement, and also what you should do with a subclass of QTreeWidgetItem to define the mimeData.

Upvotes: 3

Related Questions