PyQT model view help


Trying to move towards model-view style. Here’s how I do stuff now:

psuedo code:

class One_Item():
    def __init__(stuff...):
        self.display_name #what the user sees in a list widget

With a bunch of these objects, I’ll loop through 'em and add their display names to a list widget.

How do move over to model-view style? Do I still have these One_Item objects? Do I make them each an instance variable of a PyQt object that’s added to the ‘model’? Any pointers would help, just not sure where to start from where I’m at.


The short (and general) answer is yes, the model will contain your data (which can be a list of your custom classes), and will provide the data upon request by the view.

Longer answer can start here:



Thanks. I watched some of those before I had a problem to solve, so nothing seemed relevant. Now they’re very helpful.

Skipping ahead… I’ve got a bunch of UI elements (2 lists, 2 combo boxes). The data for all of them is currently kept in one custom class. Should I create a model for each UI element, and each would access a static custom data class? Or… One model they all use?

I also want to check what’s selected in one combo_box before getting data to populate another UI element, but is that heading down the wrong path?

(this is all probably answered in the videos… :wink:

Edit: Ok, maybe one model with a proxy model to filter out what gets displayed in each UI element.


Ok, I’ve got one model and two tree views. Do I use a unique QSortFilterProxyModel for each view, to control what’s displayed in each?


Yes, you have 2 proxy models adapting the single model for each view :slight_smile:

The reason I’m saying adapting is because the only role the model carries in the Qt framework is to act as a translator between two languages. The view speaks one and your data speaks another, and the inbetween (model) is doing all the nessesary work to adapt it from one to another, which infact is the definition of the adapter pattern :slight_smile:


Thank you!!! A simple ‘yes’ from somebody that knows what they’re talking about is all I need!


Having some trouble understanding where it’s appropriate to access the model’s data, and use it to do work.

I want to check what the user’s selected in a tree view, then work on those selections (export a mesh from maya, print something, etc). Any tips/starting points?

pseudo code guess:

class My_Model...
class Window(base, form)...

class Do_Work():
      data = (model.getSelection()).internalPointer() stuff with data...


Your view has a selectionModel which has signals you can hook into and listen for selection changes :slight_smile:

If you are using a proxy model you might need to map it back to your original model with sourceIndex = proxyModel.mapToSource( proxyIndex )


That was it! mapToSource!! I was getting crazy index out of bound errors, previously.