So what exactly is the problem?
It's easier if I draw a picture to explain what's happening.
What I want is something like this:
Attachment 8216
with the QListWidget inside the lower of the two inner QGraphicsRectItems.
What I get is the basic graphics classes drawing on the View OK, but the QListWidget is in a window of its own, in a different location, not attached to the inner rectangle. Something like this:
GraphicsItemWithoutList.JPG
Obviously, I've done something wrong, but I'm not exactly sure what. Hope this helps to clarify my problem.
You are not adding the list widget to the scene anywhere. And you certainly cannot pass a QGraphicsRectItem as a parent to QListWidget since the former is not a widget at all (dynamic_cast will return 0 here).
The code snippets I've provided don't show the addition of the Custom CommunityDisplay Class to the scene. That is done elsewhere, and pointers to the created CommunityDisplay class are added to the scene using QGraphicsScene::addItem.
I'd assumed that the QListWidget internal to the custom class would have been added to the scene with the scene.add(customclass) call I'm doing. Is this not the case?
I'd also suspected that I couldn't force the QGraphicsRectItem to be a parent to the QListWidget, so thanks for confirming that - I'll remove that. I would also guess that the main reason that the QListWidget is being displayed in its own window is that the mapToParent method won't work when the parent was invalid (0, as you pointed out in the previous reply).
I'm still a little stumped as to how to get the List to display within the custom class on the scene. Any further thoughts?
EDIT: Actually, you're absolutely right about not having added the QListWidget to the scene - all the other graphics items within the custom class have the parent scene passed in the constructor when they're newed. However, that still doesn't fix the problem, as I'm not sure there's a valid way of adding the widget to the scene, as scene.addItem requires a QGraphicsItem pointer, and I won't be able to dynamic_cast it to QGraphicsItem * from QWidget * in the same way as I couldn't dynamically cast in the opposite direction...
So, I guess the question is: Is it actually possible to add a QListWidget (or QListView) to a QGraphicsScene?
Last edited by El Bazza; 12th September 2012 at 16:12.
You don't understand. You are not adding the list widget object to the scene. It doesn't have a parent (as I explained in the previous post) and thus is not tied to the scene in any way and shows up as a separate window.
Have a look at QGraphicsProxyWidget class to see how to add a widget to a scene. However be prepared for major slowdowns if you intend to animate the item. Widgets are not supposed to be placed on graphics scenes.
El Bazza (12th September 2012)
Thanks wysota, I realised that after I'd posted the reply, hence the edit
Thanks also for the pointer to QGraphicsProxyWidget, I think I see how it should all work now. The QListWidget remains in the custom class, along with a pointer to a QGraphicsProxyWidget, which is used to display the information held within the QListWidget
EDIT: I've just checked the basics of the principle, and I now have a List Widget inside my Graphics Scene. Thanks once again, wysota![]()
Last edited by El Bazza; 12th September 2012 at 17:30.
Bookmarks