PDA

View Full Version : A few basic question



salmanmanekia
4th June 2010, 10:53
I have a few basic conceptual questions and want you guys to shed light on it as per your knowldege ,i will keep adding the new questions as i face them...:)..to start with
QGraphicsWidget
1,Is QGraphicsWidget a substitute of QGraphicsItem and can QGraphicsWidget be used instead of QGraphicsItem?
QTab
2,What i understand is that QTab can not add a widget with a Tab(i.e supposed to be done by QTabWidget) so why QTabBar is needed at all..??..

JD2000
4th June 2010, 11:10
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.

QGraphicsWidget is an extended base item that provides extra functionality over QGraphicsItem. It is similar to QWidget in many ways:

* Provides a palette, a font and a style().
* Has a defined geometry().
* Supports layouts with setLayout() and layout().
* Supports shortcuts and actions with grabShortcut() and insertAction()

Unlike QGraphicsItem, QGraphicsWidget is not an abstract class; you can create instances of a QGraphicsWidget without having to subclass it. This approach is useful for widgets that only serve the purpose of organizing child widgets into a layout.

QGraphicsWidget can be used as a base item for your own custom item if you require advanced input focus handling, e.g., tab focus and activation, or layouts.

Since QGraphicsWidget resembles QWidget and has similar API, it is easier to port a widget from QWidget to QGraphicsWidget, instead of QGraphicsItem.

Note: QWidget-based widgets can be directly embedded into a QGraphicsScene using QGraphicsProxyWidget.

- take a look at the documentation!

salmanmanekia
4th June 2010, 11:31
- take a look at the documentation!
thats where the confusion starts ,its obvious from my question that i have read docs and am confused with whats explain thr otherwise the question wud have been like What !! QWidget and QGraphicsWidget...Any ways sometime the way things are explained is not obvious to all the readers and to explain the same concept considering a certain context makes it more understandable and easy to follow...lastly,questions dont harm every one can share his idea and maybe some of us(newbie) who are not carrying the right idea can rectify before its too late ...:p

JD2000
4th June 2010, 14:07
I meant nothing by 'take a look at the documentation' except to do just that. In this case there is a lot of very detailed information that answers your question.

To summarise QGraphicsWidget inherits QGraphicsObject which in turn inherits QGraphicsItem.

So QGraphicsWidget is essentially an enhanced QGraphicsItem.

As a starting point it may be best to read the 'The Graphics View Framework' docs at http://doc.trolltech.com/latest/graphicsview.html. I hope this helps!

salmanmanekia
5th June 2010, 15:37
Thanks JD...I have a question here regarding QGraphicsWidget...
i have to make a boundary for the widget....and i am planning to use setGeometry for it...but i am wondering whats the significance of boundingRect then in QGraphicsWidget...or is it to be mostly and only used in QGraphicsItem

wysota
6th June 2010, 08:54
Geeez, man... Go read the documentation of boundingRect() and you'll see what its significance is. Are you sure you know what inheritance means? If the docs don't make everything clear and you can't employ logic to determine how setGeometry() relates to boundingRect(), then take a look at implementation of QGraphicsWidget::setGeometry().

salmanmanekia
7th June 2010, 11:46
Once again,maybe not a very interesting question for all of you but still i will go through it :p
i have to create a random number of items(for eg a rect item) in my scene and they are to be displayed in a certain way like a matrix so i was wondering how is it possible that i display them in a correct way without hardcoding the values since it depeneds on the user value...

SneakyPeterson
11th June 2010, 10:45
Howdy!
As for your boundingrect() question, it is important to understand the distinction between QGraphicsWidget and QGraphicsItem. So QGraphicsWidget inherits the bounding rect stuff from QGraphicsItem. Now, it is also important to decide whether you want to use a layout in order to display your items in the graphicsview. Because it sounds like you have a very specific implementation in mind, might I suggest you create your own QGraphicsLayout? If just having a standard layout, like a line or a graph suffices, there are already some layouts defined by QGraphicsGridLayout and QGraphicsLinearLayout. The bounding rect comes into play when using them because boundingrect helps define the space that they occupy and thus, how they are arranged by a layout. If you don't use a layout, then every time you add something to the scene you are going to have to move it yourself, which can be quite troublesome if you are managing a large number of objects.
I just implemented a QGraphicsView, and I was also a bit lost by the documentation. Let me suggest the use of layouts, if only because it makes adding QGraphicsItems to your scene very easy. If you want to add regular QWidgets you can simply create a QGraphicsProxyWidget that points to them. You then add these items to a QGraphicsLayout, which you can then set as the layout for a toplevel QGraphicsWidget. You then add the QGraphicsWidget to your QGraphicsScene, which you set as the scene for your QGraphicsView. Confusing enough :)? Well, I suppose it's so confusing because it's such a powerful tool. Remember, with great power comes extensive documentation.
When I was completely clueless as to how to work with all of this stuff I consulted the examples on the TrollTech page: http://doc.trolltech.com/4.6/examples-graphicsview.html
ALSO VERY IMPORTANT TO NOTE: your objects will not appear if you do not set size hints for them!

I hope that helped.

Cheers,
sp

salmanmanekia
11th June 2010, 13:43
Thanks Sneaky,
I am not sure if i am still in control of this idea ,things clear but ahead there lies more confusion ..:D...i will ask you a few more things ...I was reading the documentation for QGraphicsLayout and am confuse that does these layout also applies to QGraphicsItem...also can you see these lines from the doc regarding the cordinates ,i am reading it again and again and trying to figure out the logic but it just isnt happening..
Child coordinates are relative to the parent's coordinates. If the child is untransformed, the difference between a child coordinate and a parent coordinate is the same as the distance between the items in parent coordinates. For example: If an untransformed child item is positioned precisely in its parent's center point, then the two items' coordinate systems will be identical. If the child's position is (10, 0), however, the child's (0, 10) point will correspond to its parent's (10, 10) point.

SneakyPeterson
14th June 2010, 09:02
I was reading the documentation for QGraphicsLayout and am confuse that does these layout also applies to QGraphicsItem


A QGraphicsLayout is in charge of managing QGraphicsLayoutItems. QGraphicsLayoutItems can be either QGraphicsWidgets or other QGraphicsLayouts. So you can have several layouts embedded within a parent layout, interspersed with QGraphicsItems, if you so desire. Remember, if you want to use a regular widget or other graphics object that is not a QGraphicsLayoutItem, you will need to create a QGraphicsProxyWidget for it that can be added to the QGraphicsLayout instead.

Child coordinates are relative to the parent's coordinates. If the child is untransformed, the difference between a child coordinate and a parent coordinate is the same as the distance between the items in parent coordinates. For example: If an untransformed child item is positioned precisely in its parent's center point, then the two items' coordinate systems will be identical. If the child's position is (10, 0), however, the child's (0, 10) point will correspond to its parent's (10, 10) point.

The text for this is, perhaps, a little confusing. The text here is talking about coordinate transforms. It is important to remember that your layout is going to have its own size. If you customize your own layout, then you have to determine where to place items as you are adding them into the layout. When you are adding these items in your layout implementation, you are adding them with respect to the dimensions of the layout and hence the local coordinate system. Now, where this concept becomes a little bit confusing is when you add that layout to a scene (well, actually you add it to a graphicswidget which you add to the scene.) Now, you have all of these objects that have been defined by the coordinate system of the layout, right? If you don't add the layout to the origin of scene (0,0) that you're embedding them in, though, all of the objects within the layout are going to have different coordinates inside of the scene. So basically what that paragraph is saying is that while you are dealing with a QGraphicsLayout, all you have to worry about is the coordinates within the layout, and that the transforms to parent coordinate systems will be handled for you. It is important to note, though, that within the parent the coordinates will be different.

I hope that helped. Sorry for the late reply, I was out of town for the weekend and didn't have access to the interwebs :)

Cheers,
sp

genomega
16th June 2010, 05:18
I have a few basic conceptual questions and want you guys to shed light on it as per your knowldege ,i will keep adding the new questions as i face them...:)..to start with
QGraphicsWidget
1,Is QGraphicsWidget a substitute of QGraphicsItem and can QGraphicsWidget be used instead of QGraphicsItem?
QTab
2,What i understand is that QTab can not add a widget with a Tab(i.e supposed to be done by QTabWidget) so why QTabBar is needed at all..??..

As you have discovered this is not a help forum, there are many other qt forums out there and there are many helpful youtube vids.

salmanmanekia
16th June 2010, 08:57
As you have discovered this is not a help forum, there are many other qt forums out there and there are many helpful youtube vids. :eek:
What does this even means :p, and you tell me that this is not "Help Forum" and i should go there and there for it ...So pls enlighten me, whats the purpose of this forum then if not helping..?

Thanks sp,
Your response is realy appreciated...so a particular items cordinate in the layout is different from that w.r.t scene if the layout is not on (0,0) of scene ,but other wise the same item has different cordinates w.r.t layout and scene and maybe view...so that means atleast three cordinate value..Right..??..
But why and how is it useful to have a items cordinate w.r.t scene and view when its direct parent is the layout and also when and why do we have to maptoscene and mapfromscene..
-Muhammad Salman

SneakyPeterson
17th June 2010, 07:46
Your response is realy appreciated..
Happy to help :)

.so a particular items cordinate in the layout is different from that w.r.t scene if the layout is not on (0,0) of scene ,but other wise the same item has different cordinates w.r.t layout and scene and maybe view...so that means atleast three cordinate value..Right..??..

Yup. Also, the way that I use my layouts, I embed them in a QGraphicsWidget with setLayout(), so there's yet another layer of abstraction for you. Although, your scene and view coordinates should be the same if you're having your view look at the entirety of your scene.

But why and how is it useful to have a items cordinate w.r.t scene and view when its direct parent is the layout and also when and why do we have to maptoscene and mapfromscene..

Well, there's a lot of different things you can do with that kind of power. So, I just think of a scene as a very powerful and easy way to manage a large number of widgets and layouts, since it automatically passes events. Then I think of a view as a nifty little widget for viewing a scene (technically the widget part of a qgraphicsview is called the "viewport"). So you technically have an infinite number of views for just one scene, with each view corresponding to a different part of the same and thus, with it's own seperate coordinate system corresponding to a subset of the scene's coordinate system. In the same fashion, a scene can technically hold an infinite number of layouts, all placed at different parts of the scene. Thus, when you're placing items in the layout, you don't want to have to worry about the coordinate system of the parent of the layout, you just want to have to worry about the layout. Similarly, once you've placed the layouts in the scene, the propagation of events and actions and all that other stuff that you usually have to worry about is passed through automatically by the scene.
As far as the maptoscene and mapfromscene stuff goes, those functions become useful when you have several layers of abstraction in your widgets. Say you have a widget embedded in another widget embedded in a layout embedded in your scene, but for some reason you want to move that widget to the center of the scene. In order to tell where it is in the scene and how far to move it you can call those functions so that you don't need to transform the coordinates of your child widget through several different coordinate systems (parentwidget coordinates -> layoutcoordinates -> scenecoordinates).

Cheers,
sp