PDA

View Full Version : Custom ProxyModel or Two Sync'd Models ?



SSurgnier
1st August 2011, 22:22
I have image information stored in a text csv file such as:

#xPos,yPos,grayScaleValue
0,0,0.01
2,3,0.125
4,3,0.500
#etc...

I have two views split vertically. The left view is a standard QTableView and the right view is a custom ImageView (where I will be manually painting the appropriate squares). The TableView's source model is simply the csv file. I need to transform the xPos,yPos data from the source model into actual row,col of a new model which will be the source for the ImageView. So my question is whether I should write a custom ProxyModel to "transform" the source model or to just have two separate synchronized models. The separate model solution intuitively doesn't seem correct since all required data can be referenced from the source model, but I'm having mucho difficulty writing the custom ProxyModel. Specifically, the ::mapFromSource and ::MapToSource functions. Guidance on this dilemma will be much appreciated.

-Steven

Santosh Reddy
2nd August 2011, 03:15
Well you could make it all work with one model. As you said you already have a standard QTableView (I hope is working, else you may want to fix this first), and then you have custom view, you could do all co-ordinate generation / transformation in the custom view itself.

IMO proxy models are generally required, if you want to have multiple views to same base model, with slightly (map-able) different visualization. In your case the visulization is generated based on the data itself, so it better to have the position translation in your custom view. And as always try to restrict the custom view to use the QAbstractItemModel interface as much as possible, as a good practice, don't use custom calls / signals / slots between the custom view and model (you might face synchronization issues later on in the development)

You could explore using a item delegate, which may simply your custom view's complexity.

SSurgnier
2nd August 2011, 18:44
Thanks for the advice Santosh. I'm going to abandon the proxy model for now. The reason I started down the path of developing the proxy model was because when I started writing the custom view I kept falling into using a 2D array to hold the desired info and then iterating the through the 2D array and printing it to the screen. I'm new to the MVC architecture. So when I started building the 2D array as the basis for the the custom view from the source model it just felt wrong- after reading several MVC tutorials, I had it ingrained in my head to use a model behind my view to simplify co-selection between the source model table view and the custom view. I'll continue with the aid of your suggestion and hopefully not run into any synchronization issues between the 2D array and source model. Would you please elaborate on your item delegate suggestion?