Results 1 to 14 of 14

Thread: Problem in attachig two polygons

  1. #1
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem in attachig two polygons

    Hi,
    I am doing one application in which, i wrote one class A to create different shapes of polygons. Then i wrote another class B for creating a special polygon which i have to attach with polygons of class A dynamically. Before i did somethinig similar but with QGraphicsItem class using setPos() function for attaching one polygon with another. But with QPolygon, it is working similar. Please suggest me some way.

    Regards
    Rohit

    This is the constructor of class A

    Qt Code:
    1. A::A(DiagramType diagramType, QMenu *contextMenu,
    2. QGraphicsItem *parent, QGraphicsScene *scene)
    3. : QGraphicsPolygonItem(parent, scene)
    4. {
    5. myDiagramType = diagramType;
    6. myContextMenu = contextMenu;
    7.  
    8.  
    9. switch (myDiagramType) {
    10.  
    11. case StartEnd:
    12. path.moveTo(200, 50);
    13. path.arcTo(150, 0, 50, 50, 0, 90);
    14. path.arcTo(50, 0, 50, 50, 90, 90);
    15. path.arcTo(50, 50, 50, 50, 180, 90);
    16. path.arcTo(150, 50, 50, 50, 270, 90);
    17. path.lineTo(200, 25);
    18. myPolygon = path.toFillPolygon(); //myPolygon is an object QPolygonF class
    19. break;
    20.  
    21. case Conditional:
    22. myPolygon << QPointF(-100, 0) << QPointF(0, 100)
    23. << QPointF(100, 0) << QPointF(0, -100)
    24. << QPointF(-100, 0);
    25. break;
    26.  
    27. case Step:
    28. myPolygon << QPointF(-100, -100) << QPointF(100, -100)
    29. << QPointF(100, 100) << QPointF(-100, 100)
    30. << QPointF(-100, -100);
    31.  
    32. break;
    33.  
    34. default:
    35. myPolygon << QPointF(-120, -80) << QPointF(-70, 80)
    36. << QPointF(120, 80) << QPointF(70, -80)
    37. << QPointF(-120, -80);
    38. break;
    39. }
    40.  
    41. anditem = new AndItem();
    42.  
    43. ///// What code to write to attach andItem->myPolygon to myPolygon of this class.////
    44.  
    45.  
    46. setPolygon(myPolygon);
    47. setFlag(QGraphicsItem::ItemIsMovable, true);
    48. setFlag(QGraphicsItem::ItemIsSelectable, true);
    49.  
    50. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 8th May 2007 at 11:59. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in attachig two polygons

    Take the painter path of the first polygon (a QPainterPath ) and use QPainterPath::addPath to add your second polygon ( which I assume is also a QPainterPath ).

    Next, set the shape back to the first item. This is what I understand by "attached".
    I don't know how convenient is this for you.
    Maybe you don't want to modify the geometry of your items.

    Regards

  3. #3
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in attachig two polygons

    Thanks Marcel...

    Now i try what you suggested but before just to tell u what i want as output. An example jpg is attached with two polygons.

    Reg
    Rohit
    Attached Images Attached Images

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in attachig two polygons

    In this case you should try another approach.

    1. Take the bounding box of the first polygon.
    2. Take the bounding box of the second polygon
    3. Move the second polygon such that is left bbox coordinate is equal to the right bbox coordinate of the first polygon.

    Keep in mind that you may not get acceptable results for complex polygons.

  5. #5
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in attachig two polygons

    Dear Marcel......

    I added these lines in the same constructor of class A, because of the first two points you mentioned in the last post.


    myGateRect= myPolygon.boundingRect();// QRectF myGaterect;
    anditem = new AndItem();
    myPolygonConnector =anditem->polygon(); // QPolygonF myPolygonConnector;
    myConnectorRect= myPolygonConnector.boundingRect();//QRectF myConnectorRect;

    I am not able to proceed aftre this.
    Please help..
    Rohit

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in attachig two polygons

    Next you have to bring the items next to each other by using either moveBy or setPos, whatever suites you.

  7. #7
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in attachig two polygons

    Marcel

    Does "setPos" or "MoveBy" works with QPolygon object. Sorry if i ask some silly question. Because it is not working for me.

    Rohit

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in attachig two polygons

    No, I meant QGraphicsItem::setPos or QGraphicsItem::moveBy.
    You should work directly with the graphics items, and not the shapes themselves. Perhaps create a group out of A and B?

    Regards

  9. #9
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in attachig two polygons

    Dear Marcel

    Yes Marcel, I was trying to use setPos with polygon :-) I am silly.... But these classes are derived from of QGraphicsItem. So now setPos is fine...But my problem is not solved because
    ....
    I dont have the screen coordinates so that i can move them or set their position because the QPolygonF item is placed on the screen at run time. This is handled in the mouse press events of the screen window class. So when at run time object is created, it should have these polygon connected (As forexample gate with connector).

    If its ok for you then i send my code.

    Rohit

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in attachig two polygons

    Sure, it is better to post the code.

    Regards

  11. #11
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in attachig two polygons

    Dear Marcel
    Now i am at home....:-)
    I tried to attach complete code but it is big. So i am attaching five files. "This code is similar to the example in QT4.3 examples but a little difference"
    Explanation :- The application has buttons on the left when the user click any button and then click in the screen window the polygon is pasted there whose picture was displayed on the button clicked. But i want that polygons in DiagramItems works as different gates and i can add any number of connectors at run time to that. But standard are one input and two output connectors(polygon) with a gate(Polygon).
    So i created a class "Gate.cpp" (Attached) which contains AndItem class which creates a polygon works as connector to gate and i tried to create a run time object of AndItem class mentioned in "Gate.cpp" file in the constructor of the DiagramItem class which creates diffrent types. Now if the user click the button and then click the screen window there are two polygon pasted which will together as Gate polygon and connector polygon. Then i will modify for connecting two polygons with arrow.
    I am new to Qt and any help ,i will take it gladly.
    Thanks for your guidance Marcel.
    Sorry if i give you any trouble.
    Rohit
    Attached Files Attached Files

  12. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in attachig two polygons

    Hello,

    I assume you are trying to make some kind of electronic circuits designer.
    I'm afraid that your approach is a little too simple.

    You should keep account of your ports.
    For example, let's say you have an AND port in the scene.
    Next, you add an OR port and you want to connect it to the output of the AND port.
    Initially the OR port will be placed somewhere in the scene. You drag it with the mouse and when you are near the output of the and the OR will snap to the AND port and automatically a connector will be created.

    By snap I mean that one input of the OR port will be aligned to the output of the AND gate.
    If you drag it further the connector disappears.

    All I am trying to say is that your app should be more interactive.

    Keeping account of the items is not hard. You always can access them via QGraphicsScene::items.

    When you drag a port you just look at the other items near it, to see if you can connect to one of the them ( maybe define a snap distance ).

    Regards

  13. The following user says thank you to marcel for this useful post:

    rohitjun (9th May 2007)

  14. #13
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in attachig two polygons

    Thanks alot for such a precious reply Marcel

    You are perfectly right that i am trying ot build a electronic desing application.

    yes i completely agree with you that my application should be more interactive.

    But the ports will not be attached automatically when they come near to each other.The user has to click one of the connector output of the "suppose" OR port and the drag the mouse till the inut connector of "suppose" And port. Then the connection between the two ports will be established.

    I thought of a approach, " When the user put a port on the scene then it will have one input and two output connector. But if the user wish to add or delete any connector he has to right click and then add or delete the connector through pop up menu. So in the begining i am trying to create a Port with connectors which user can put on the scene window. Connector polygon will handle the wiring between ports."

    Please give some suggestions on it.
    If you think , i am wrong way, please tell any other approach.

    Regards
    Rohit

  15. #14
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in attachig two polygons

    Dear Marcel

    Good morning

    I tried to make two polygons join together. But setPos,moveBy doesnt work for me as well as i tried to combine directly when i use setPolygon(First polygon+ second polygon) function but this create one polygon over another.
    Now i am thinking of not to use connector class. I think i can set some points on the Port(AND) itself which can be used to connect to other ports. It means any port will connect to new port with a new point for wiring whenever the user will wire to a new port.

    Please suggest,

    Rohit

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.