Results 1 to 16 of 16

Thread: How to reflect QGraphicsItem?

  1. #1
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to reflect QGraphicsItem?

    I have a task to reflect one or more QGraphicsItems relatively to a line. And this operation should be performed correctly again and again to the mirrored items.
    I've read a lot about QTransform and affine transformations and realized that I need 1) rotate; 2) scale (1;-1) 3) translate..somewhere
    that's what I have now:
    Qt Code:
    1. QGraphicsLineItem* line = qgraphicsitem_cast<QGraphicsLineItem*>(curItem);
    2. double x1 = line->line().x1();
    3. double y1 = line->line().y1();
    4. double x2 = line->line().x2();
    5. double y2 = line->line().y2();
    6. double angle = atan((y2 - y1)/(x2 - x1));
    7. QMessageBox::warning(0, "Scribble", "angle "+QString::number(angle*180/acos(-1.0)));
    8. group->rotate(angle*180/acos(-1.0));
    9. group->scale(1,-1);
    10. group->rotate(-angle*180/acos(-1.0));
    11. foreach (QGraphicsItem* item,group->children())
    12. item->setTransform (group->transform(),true);
    13. destroyItemGroup(group);
    14. removeItem(line);
    15. delete line;
    16. curItem = NULL;
    To copy to clipboard, switch view to plain text mode 
    This is steps 1-2 as I don't know how to translate items correctly regardless of their pos() and previous mirror operations. At least the resulting angle of mirrored item seems to be right, but not after the second reflection
    "curItem" holds the line of the mirror (it's temporal and is erased after the operation), "group" of items to be reflected is created elsewhere.
    Thanks in advance for all your help. I attached the whole project (Qt 4.3.2, Visual Studio 2005 .vcproj, but without a .pro, sorry)
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to reflect QGraphicsItem?

    Will have to see into ur prob,,,, seems interesting.
    Meanwhile you can have a look at this

  3. #3
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to reflect QGraphicsItem?

    The link has no deal with my question

  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: How to reflect QGraphicsItem?

    As far as I understand mirroring without taking a piece of paper and experimenting with calculations, mirroring is equal to scaling with a scale of (1, -1) or (-1, 1) where the scale axis (x or y) matches the mirror line. Therefore all you need to do can be summarized by three points:
    1. transform the coordinate space so that one of its axis matches the line of the mirror
    2. scale relative to the chosen axis
    3. transform the coordinate space back to its original state

    Remember that you want to move the coordinate space and not the item, so you should probably invert all the matrices if you want to apply them to the item and not the world.

    I suggest you start by creating a custom widget derived from QWidget that will consist of a shape (even hardcoded), a mirror line (hardcoded) and a paint event that will transform the painter and draw the mirrored shape. This will let you verify my assumptions and should allow you to do the reverse mapping you need with the graphics view architecture.

  5. #5
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to reflect QGraphicsItem?

    Thanks but I need reflecting not only relatively to an axis, but to ANY line, given by 2 points.

  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: How to reflect QGraphicsItem?

    See points 1. and 3. of my previous post.

  7. #7
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: How to reflect QGraphicsItem?

    I still keep on failing with this task...
    In the constructor of the scene I've placed 2 lines
    Qt Code:
    1. QGraphicsLineItem* line = new QGraphicsLineItem(0,0,400,0);
    2. QPen pen(Qt::SolidLine);
    3. pen.setWidth(2);
    4. pen.setColor(Qt::black);
    5. line->setPen(pen);
    6. addItem(line);
    7. line = new QGraphicsLineItem(0,0,0,400);
    8. addItem(line);
    To copy to clipboard, switch view to plain text mode 
    they'll be axises. Also i track mouse coordinates
    Qt Code:
    1. QPoint p = mouseEvent->scenePos().toPoint();
    2. QGraphicsView* view = views()[0];
    3. QMainWindow* parent = qobject_cast<QMainWindow*>(view->parentWidget());
    4. parent->statusBar()->showMessage(QString::number(p.x())+" ; "+QString::number(p.y()));
    To copy to clipboard, switch view to plain text mode 
    why when I just create simple line with dots (10,10) and (60,10) and then call
    item->translate(0, 20);
    it moves to the position (10,50) and (60,50) ?!

    Well, that's not essential for me at this moment...
    After hours of mind torturing I've written this:
    Qt Code:
    1. QGraphicsLineItem* line = qgraphicsitem_cast<QGraphicsLineItem*>(curItem);
    2. double x1 = line->line().x1();
    3. double y1 = line->line().y1();
    4. double x2 = line->line().x2();
    5. double y2 = line->line().y2();
    6. double angle = atan((y2 - y1)/(x2 - x1));
    7. foreach (QGraphicsItem* item,group->children())
    8. {
    9. QTransform tozero;
    10. tozero.translate(-x1/2, -y1/2);
    11. tozero.rotateRadians(angle);
    12. QTransform mirror;
    13. mirror.scale(1, -1);
    14. QTransform back;
    15. back.rotateRadians(-angle);
    16. back.translate(x1/2, y1/2);
    17. item->setTransform(tozero*mirror*back,true);
    18. }
    To copy to clipboard, switch view to plain text mode 
    It works perfectly relativly to any line that is paralle to 0x, but if it's not the item flies away =( help me plz!
    Last edited by Radagast; 26th June 2008 at 18:38.

  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: How to reflect QGraphicsItem?

    So why not try my approach?

  9. #9
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to reflect QGraphicsItem?

    wysota
    as far as I understand, it IS your approach

  10. #10
    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: How to reflect QGraphicsItem?

    Certainly not. I told you to transform the viewport and not the item. I really suggest leaving graphics view aside now and finding proper transformations using QPainter - it should be much simpler. Then you can adjust the code for the graphics view architecture. Currently you are just guessing.

  11. #11
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to reflect QGraphicsItem?

    hmm but why should I transform the viewport? o_O I can have many items in the viewport and not all of them must be mirrored, but only the selected ones.
    And how the "QPainter decision" can be simpler...then I'll have to convert it to the "GVF decision". I almost have this decision, it almost works, I'm sure the trouble is very tiny, but I can't see it ((
    anyway thanks for your attention...

  12. #12
    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: How to reflect QGraphicsItem?

    Yes, you are right - almost.

  13. #13
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to reflect QGraphicsItem?

    So if you clearly see my error, why don't just tell it? and get one more "thanks"
    u see I'm not of the ones who ask to do everything for them, I did my best!

  14. #14
    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: How to reflect QGraphicsItem?

    I don't see your error. I suggest you experiment with a simpler world, but you know better, so stick to your approach.

  15. #15
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to reflect QGraphicsItem?

    even if a man with such a long operational experience as you have (as I suggest) doesn't see the error...how wil I see it? it's my 1st program in Qt.

  16. #16
    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: How to reflect QGraphicsItem?

    Maybe because I didn't look for it.

Similar Threads

  1. Disable default tab behaviour for a QGraphicsItem
    By nmather in forum Qt Programming
    Replies: 3
    Last Post: 13th December 2017, 10:30
  2. QGraphicsItem doesn't inherit QObject?
    By xyzt in forum Qt Programming
    Replies: 6
    Last Post: 26th September 2011, 14:59
  3. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  4. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  5. Snap-to grid on QGraphicsItem
    By hardgeus in forum Qt Programming
    Replies: 9
    Last Post: 28th April 2008, 16:22

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.