Results 1 to 9 of 9

Thread: Custom QGraphicsItem - Adding a QListWidget

  1. #1
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Custom QGraphicsItem - Adding a QListWidget

    I've created a custom QGraphicsItem, inheriting from QGraphicsRectItem, to be precise. This custom class contains (at the moment) two other QGraphicsRectItems, one bounding a QGraphicsTextItem as a holder for a Name, and the other QGraphicsRectItem is added to act as a placeholder for a List of information. So far, so good...

    I've tried to add a QListWidget, but cannot get list information to display correctly within the bounds of the QGraphicsRectItem that I've set as its parent.

    I know I'm doing something wrong, and perhaps what I'm attempting isn't really possible with QGraphicsScene/QGraphicsView?

    Either way, if anyone can steer me in an appropriate direction, I'd be most grateful!

    Using Qt Creator 1.3.1, Qt 4.6.2.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QGraphicsItem - Adding a QListWidget

    Where did you try to add QListWidget? Why did you inherit the item class instead of just composing items using parent-child relationship?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom QGraphicsItem - Adding a QListWidget

    It's probably easier if I add code snippets to show what I've done:

    Header File Info:

    Qt Code:
    1. class CommunityDisplay : public QGraphicsRectItem
    2. {
    3. // Attributes
    4. public:
    5.  
    6. protected:
    7. QListWidget *m_pListView;
    8.  
    9. private:
    10. QGraphicsRectItem *m_pNameRectItem;
    11. QGraphicsRectItem *m_pListRectItem;
    12.  
    13. //Methods
    14. public:
    15. CommunityDisplay(QGraphicsScene *pParentScene,
    16. qreal xPos,
    17. qreal yPos,
    18. QString strName);
    19.  
    20. };
    To copy to clipboard, switch view to plain text mode 

    Source Code for the Constructor

    Qt Code:
    1. CommunityDisplay::CommunityDisplay(QGraphicsScene *pParentScene,
    2. qreal xPos,
    3. qreal yPos,
    4. QString strName)
    5. {
    6. // Set up the Outer Rectangle for the shape
    7. setRect(xPos, yPos, 225.0, 250.0);
    8. QRectF Rect = rect();
    9. QPoint point;
    10.  
    11. // Set up the inner QGraphicsRectItem for the Name area
    12. m_pNameRectItem = new QGraphicsRectItem(this, pParentScene);
    13. m_pNameRectItem->setRect(Rect.x() + 5.0,
    14. Rect.y() + 5.0,
    15. Rect.width() - 10.0,
    16. 45.0);
    17.  
    18. // Set up the inner QGraphicsRectItem for the List
    19. m_pListRectItem = new QGraphicsRectItem(this, pParentScene);
    20. m_pListRectItem->setRect(Rect.x() + 5.0,
    21. Rect.y() + 55.0,
    22. Rect.width() - 10.0,
    23. Rect.height() - m_pNameRectItem->rect().height() - 15.0);
    24.  
    25. // Set up the Name Text Item
    26. m_pName = new QGraphicsTextItem(strName, this, pParentScene);
    27. m_pName->setPos(m_pNameRectItem->rect().x() + 5.0,
    28. m_pNameRectItem->rect().y() + 2.0);
    29.  
    30. // Set up the List Widget within the bounds of its QGraphicsRectItem
    31. m_pListView = new QListWidget(dynamic_cast<QWidget *>(m_pListRectItem));
    32. m_pListView->setBaseSize(m_pListRectItem->rect().width() - 4,
    33. m_pListRectItem->rect().height() - 4);
    34.  
    35. m_pListView->setViewMode(QListView::ListMode);
    36. m_pListView->setFlow(QListView::TopToBottom);
    37. m_pListView->setSelectionMode(QListView::SingleSelection);
    38. // m_pListView->setViewport(dynamic_cast<QWidget *>(m_pCryptosListRectItem));
    39. point.setX((int)(m_pCryptosListRectItem->rect().x() + 2));
    40. point.setY((int)(m_pCryptosListRectItem->rect().y() + 2));
    41. m_pListView->mapToParent(point);
    42. m_pListView->setEnabled(true);
    43. m_pListView->setShown(true);
    44. m_pListView->setVisible(true);
    45.  
    46. // DEBUG Attempt to see if the list widget will be displayed
    47. m_pListView->addItem("Unit1");
    48.  
    49. setActive(true);
    50. setEnabled(true);
    51. setFlag(QGraphicsItem::ItemIsMovable);
    52. setFlag(QGraphicsItem::ItemIsSelectable);
    53. }
    To copy to clipboard, switch view to plain text mode 

    Hope this helps to clarify what I'm trying to do.

    PS I know that adding an item to the list widget in the constructor of the class is naughty, I'm just doing it to make sure the list view is actually visible, and I'll concentrate on adding items to the list correctly once I can get it inside the rectangle it's supposed to be in

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QGraphicsItem - Adding a QListWidget

    So what exactly is the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom QGraphicsItem - Adding a QListWidget

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QGraphicsItem - Adding a QListWidget

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom QGraphicsItem - Adding a QListWidget

    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 15:12.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QGraphicsItem - Adding a QListWidget

    Quote Originally Posted by El Bazza View Post
    The code snippets I've provided don't show the addition of the Custom CommunityDisplay Class to the scene.
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    El Bazza (12th September 2012)

  10. #9
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom QGraphicsItem - Adding a QListWidget

    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 16:30.

Similar Threads

  1. (PyQt4) QtGui QListWidget adding new item problem
    By mshemuni in forum Qt Programming
    Replies: 0
    Last Post: 4th September 2012, 04:05
  2. QListWidget, help adding new items.
    By JeremyRussell in forum Newbie
    Replies: 4
    Last Post: 6th April 2011, 23:54
  3. Replies: 1
    Last Post: 22nd February 2010, 10:53
  4. adding QGraphicsItem to multiple scenes
    By forrestfsu in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2006, 14:25
  5. [Qt4]: Adding centered items in QListWidget
    By Jojo in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2006, 20:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.