Results 1 to 8 of 8

Thread: QGraphicsItem geometry change implementation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QGraphicsItem geometry change implementation

    I am trying to figure out how objects in a QGraphics Scene/View can be manipulated and redrawn.

    From what I have learnt so far, when a QGraphicsItem needs to be added to the scene,

    1. The object is derived from QGraphicsItem.
    2. boundingRect(), and paint() needs to be reimplemented.
    3. If some of the sizes need to be changed, use prepareGeometry(), and update().

    So, I am working with two actions. One to add a box to the scene, and the other to increase the size when selected.

    I tried to get it to work in one application and it did not work.(diagram.zip). The sizes of the item seem to reset before painting. I can see this if I keep increasing the size and trap the code. However, under certain paths it seem to work. For example, if I add a child to a box and select the parent, the size of the parent can be changed programmatically.

    However, I created the minimum code required and it worked.
    The code is in GrScene.zip

    Why is there a difference in behavior?

    Thanks in advance.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem geometry change implementation

    In this code:

    Qt Code:
    1. void DiagramWindow::increaseSize()
    2. {
    3.  
    4. QList<QGraphicsItem *> items = scene->selectedItems();
    5. if (items.count() == 1) {
    6. Node *selNode = dynamic_cast<Node *>(items.first());
    7. selNode->increaseSize();
    8. }
    9. return;
    10. }
    To copy to clipboard, switch view to plain text mode 

    What happens if the selected node is a child of another node? I think in that case, both nodes will be marked as selected so your count will be 2, not 1. Have you looked at the size of the count in the cases where "works" and "doesn't work"?

  3. #3
    Join Date
    Mar 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem geometry change implementation

    The count does not seem to matter. However, the path it gets there to increase the size seems to.

    MainWindow::increaseSize() --> Node::increaseSize() - does not work. Actually it resets the size. No idea how that happens
    MainWindow::selectedNode() --> Node::increaseSize() - seems to work.


    If I go through the second path and try the first path, the sizes get reset.

    If I try to trace the code, it goes into the moc_ code. I am wondering if some of the variables are initialized here. I cannot trace the code all the way.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem geometry change implementation

    The count does not seem to matter.
    Of course the count matters. Your code (in either example) will do nothing if the selected node count is not exactly 1.

    In your DiagramWindow:: selectedNode() code, you -are- resetting the node size to nodeh x nodew when the node has no children. And in addChild(), since you call this method -before- you add a child to it, you are resetting the size there as well.

    I'm not sure why you wrote selectedNode() that way, but it looks like some confused thinking about what should happen when you add a child node.

Similar Threads

  1. the minimized dialog can not change the geometry?
    By wearilybird in forum Qt Programming
    Replies: 0
    Last Post: 23rd September 2014, 07:36
  2. Change implementation according to different platform
    By stereoMatching in forum Qt Quick
    Replies: 1
    Last Post: 27th December 2013, 15:28
  3. Replies: 0
    Last Post: 8th July 2013, 13:44
  4. Can't change QHBoxLayout geometry
    By gutiory in forum Newbie
    Replies: 6
    Last Post: 17th June 2010, 08:37
  5. GraphicsView: Notifying geometry change to parent group
    By Gopala Krishna in forum Qt Programming
    Replies: 8
    Last Post: 12th August 2007, 12: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
  •  
Qt is a trademark of The Qt Company.