PDA

View Full Version : Using QGraphicsView with model/view programming



JLP
29th January 2007, 10:28
Hi,

I'm new here and also quite new to Qt4 programming. So far I didn't have any problems with Qt, it is very nice and easy to work with. But now I have stumbled upon a problem, that I'm not sure how to solve best and I hope someone will be able to help me.

I've started working on a KDE4/Qt4 client called Parsek (http://www.thousandparsec.net/tp/dev/documents/parsek/) for playing Thousand Parsec (http://www.thousandparsec.net/tp/) games (similar to Stars! or Galactic Civilizations). In the game you connect to server and you can fetch a list of objects (like stars, planets, spaceships/fleets) in the universe. These objects can be stored in Qt's model/view programming classes.

Now I would like to use this model with objects and display the objects in 2D using QGraphicsView and related classes in addition to treeview of the universe. What I would like to know is if this is the right way to go here. I did quite some reading about model/view programming and QGraphicsView and as far as I could understand QGraphicsView wasn't designed to be simply plugged in as view for the models.

So it would be great if someone could tell me if I'm thinking in the right direction and if there are any tips or tricks for making this work together. Are there any tutorials that would be usefull in my case. Thanks in advance for all your help.

wysota
29th January 2007, 10:57
I believe this is you who posted on Johan's blog some time ago, so welcome to QtCentre :)

You're right, graphics view can't be connected to Interview right away, but you can surely do that and it's a good way to go. I don't think there are any tutorials describing the actual process, but it shouldn't be too hard to implement a connection between the two. Basically you have two choices:
1. Keep the graphicsview data within the model
2. Keep references to the model in the scene/items

Depending which one you choose I believe you'll face different issues.

JLP
29th January 2007, 11:14
Yes, that was me posting on blog. After that I didn't have much time to work on Parsek so I didn't look much into it after that. Now I have about two weeks of free time and I plan to use this time learning and working on this.

So if I understand in your articles about OpenGL (great stuff, thanks a lot for it) you use the first way of doing things, with data kept in custom user roles in model?

I guess I'm still too much of a beginner to see for real what advantages and dis-advantages any of these two choices would bring Would it be possible if you could point them out? Is there any one way that is easier to do/understand for a newbie like me?

Thanks again for your help.

wysota
29th January 2007, 12:04
you use the first way of doing things, with data kept in custom user roles in model?
No, I'm using a completely different approach, don't compare those. I only have a single "database", whereas you have two which have to blend together.


I guess I'm still too much of a beginner to see for real what advantages and dis-advantages any of these two choices would bring. Would it be possible if you could point them out?
I can't without experimenting.


Is there any one way that is easier to do/understand for a newbie like me?
I think noone has tried such a thing before so it's all white spots on the map.

It also depends if the scene representation is to be a separate model. If so, I'd probably embed the scene into the model and fetch data directly from the scene and the items themselves (this would give you a simmilar interface to QStandardItemModel). But if it is to be part of a greater model, I'd probably subclass the scene (or something else) and provide a translation between the model indexes and graphics items (that would be the second approach).