Results 1 to 11 of 11

Thread: Question about QGraphicsRectItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about QGraphicsRectItem

    Quote Originally Posted by lni View Post
    In QWidget, we never have to worry about where is the parent's location, we don't care, because we know the child position is only related to its parent...
    This is exactly the case with graphics items as well.

    But not in QGraphicsItem, becasue all item's cordinates have the same origin, which is the scene's origin...
    No, this is not true. You are simply using the item in a wrong way. QGraphicsRectItem::setRect() is not equivalent to QWidget::setGeometry()!

    Substitute your testGraphicsRect() with this one:
    Qt Code:
    1. static void testGraphicsRect()
    2. {
    3. // test graphics
    4. scene->setSceneRect( 0, 0, 500, 500 );
    5.  
    6. outerRect->setRect( QRectF( 0, 0, 400, 400 ) );
    7. outerRect->setBrush( QBrush( Qt::black ) );
    8.  
    9. innerRect->setRect( QRectF( 0, 0, 200, 400 ) );
    10. innerRect->setParentItem( outerRect );
    11. innerRect->setPos(5,5);
    12. innerRect->setPen( QPen( Qt::red ) );
    13. innerRect->setBrush( QBrush( Qt::red ) );
    14.  
    15. scene->addItem( outerRect );
    16. outerRect->setPos(20,20);
    17. QGraphicsView* view = new QGraphicsView(scene);
    18.  
    19. view->setWindowTitle( "testGraphicsRect" );
    20. view->show();
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question about QGraphicsRectItem

    Quote Originally Posted by wysota View Post
    You are simply using the item in a wrong way. QGraphicsRectItem::setRect() is not equivalent to QWidget::setGeometry()!
    Maybe the document for QGraphicsRectItem::setRect() should be more clear, I did think it was the same as setGeometry and use it as setGeometry, and now I have to toss my codes...

    Another question about setPos and translate: it appears that both functions have exactly the same visual effect. I did scale and rotate after setPos and translate, both produce identical view. Can you give example where they will produce different result after some operations? I will be using a lot setPos and/or translate, I hope you can give more detailed info on them so I don't make mistake again...

    Also, what is the chance to have QGraphicsAbstractBuilder and QGraphicsBuilder, just as QFormBuilder? Serializing QGraphicsScene and its content is very useful...

    Many thanks

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

    Default Re: Question about QGraphicsRectItem

    Quote Originally Posted by lni View Post
    Maybe the document for QGraphicsRectItem::setRect() should be more clear, I did think it was the same as setGeometry and use it as setGeometry, and now I have to toss my codes...
    Please don't blame the docs, they are very clear about this, it's just you didn't read them carefully enough.

    The Graphics View coordinate system.

    Another question about setPos and translate: it appears that both functions have exactly the same visual effect.
    Yes, that's correct.
    I did scale and rotate after setPos and translate, both produce identical view. Can you give example where they will produce different result after some operations?
    QGraphicsItem::pos() will probably return a different value which can be very important in many situations.

    I will be using a lot setPos and/or translate, I hope you can give more detailed info on them so I don't make mistake again...
    Use setPos() and forget about translate(), it's purely for completeness.

    Also, what is the chance to have QGraphicsAbstractBuilder and QGraphicsBuilder, just as QFormBuilder? Serializing QGraphicsScene and its content is very useful...
    None, I guess. There is no data format for storing graphics items. The easiest way to do serialization is to use QDataStream.

  4. #4
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question about QGraphicsRectItem

    Quote Originally Posted by wysota View Post
    Please don't blame the docs, they are very clear about this, it's just you didn't read them carefully enough.
    Yes, it is my careless. People tend to skip most part of document.
    Quote Originally Posted by wysota View Post
    Use setPos() and forget about translate(), it's purely for completeness.
    setPos and translate are very very different and produce totally different results after several scale and translate operations. It took me 4 hours to finally find out that I should be using setPos!


    You are very helpful! Thank you!

Similar Threads

  1. Plugin implementation question
    By JPNaude in forum Qt Programming
    Replies: 12
    Last Post: 27th August 2008, 20:24
  2. SQL Question
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 19:36
  3. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49
  4. Replies: 1
    Last Post: 15th March 2007, 20:45

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.