Results 1 to 14 of 14

Thread: need help with QGraphicItem

  1. #1
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default need help with QGraphicItem

    Hi all,

    I have problem working with QGraphicItem. I am drawing a triangle with sides,(a=84,b=84and c=116) I dont know how to specify the bounding Rect() and shape()

    QRectF Traingle::boundingRect() const
    {
    //return QRectF(, , , ,); //What value should be specified here ??
    }


    QPainterPath Triangle::shape() const
    { QPainterPath path;
    //what should be written here?
    return path;
    }

    Actually I want to rotate the triangle, but not sure how to specify the boundingRect() and shape(), can some body please explain?
    Rachana

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: need help with QGraphicItem

    Hi, maybe you could make use of QGraphicsPolygonItem? It would calculate the bounding rect and the shape for you. Anyway, you can use QPainterPath to calculate both:

    Qt Code:
    1. QRectF Triangle::boundingRect() const
    2. {
    3. return shape().boundingRect();
    4. }
    5.  
    6. QPainterPath Triangle::shape() const
    7. {
    8. // QPointF p1, p2 and p3 are the points of the triangle
    9. path.addPolygon(QPolygonF() << p1 << p2 << p3);
    10. return path;
    11. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need help with QGraphicItem

    hi I am able to draw the triangle, but I want to rotate it on right click, even though it rotates it
    never rotates on the centre it moves all over the place,

    can some body give hint like whats wrong here? I am attaching the source file,
    Attached Files Attached Files

  4. #4
    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: need help with QGraphicItem

    Nothings wrong... just the centre axis...
    The items are rotated on axis of (0,0). So when you right click, they are rotated in relation to (0,0).

    Just change your points in shape() and boundingRect() to -
    QPointF p1(0,-40);
    QPointF p2(-40,40);
    QPointF p3(40,40);
    Now (0,0) will be in centre of the triangle and you will get the result you want

  5. The following user says thank you to aamer4yu for this useful post:

    rachana (29th January 2007)

  6. #5
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need help with QGraphicItem

    But while rotating the triangle , the lines of the triangle becomes hazy,
    can some body suggest how to solve this? like this
    Attached Images Attached Images
    Last edited by rachana; 29th January 2007 at 12:57. Reason: added image

  7. #6
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: need help with QGraphicItem

    You just need to enable antialiasing in the view. Add this in your MainWindow's constructor.
    Qt Code:
    1. view->setRenderHint(QPainter::Antialiasing);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Gopala Krishna; 29th January 2007 at 15:38. Reason: missing [code] tags
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  8. #7
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need help with QGraphicItem

    I tried still facing the same problem

  9. #8
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: need help with QGraphicItem

    But it showed up properly when I tried it. Are you sure there is no improvement ? Can you please post the pic now for comparison ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #9
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need help with QGraphicItem

    here is the pic and code I have modified,



    view = new QGraphicsView(scene); //make sure always pass the QGraphicsScene
    view->setSceneRect(-400,-400,800,800);

    view->setRenderHint(QPainter::Antialiasing);
    setCentralWidget(view);
    Attached Images Attached Images

  11. #10
    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: need help with QGraphicItem

    Set the antialiasing in the paint function of traingle class...

    painter->setRenderHint ( QPainter::Antialiasing);

  12. #11
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need help with QGraphicItem

    It is also set I dont know why the sides of triangle becomes jagged once its rotated

  13. #12
    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: need help with QGraphicItem

    its working fine for me...
    see the a attached pic..

    Are they smooth in ur perception ?? If not what exactly do u mean by jagged edges ?

    Also can u post ur Triangles:aint() function ??
    Attached Images Attached Images
    Last edited by aamer4yu; 30th January 2007 at 05:46.

  14. #13
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need help with QGraphicItem

    void Triangle:: paint ( QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
    {

    painter->setRenderHint(QPainter::Antialiasing);
    //need to give some colors & may use some gradient here
    painter->setPen(QPen(Qt::black,0, Qt::SolidLine,Qt::FlatCap, Qt::MiterJoin));
    QPointF p1(0,-40);
    QPointF p2(-40,40);
    QPointF p3(40,40);
    painter->drawPolygon(QPolygonF() << p1 << p2 << p3);

    }

    I mean to say the sides of triangle never appears straight on rotation, they appear like zig zag pattern

  15. #14
    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: need help with QGraphicItem

    does the snapshot i posted appear jagged ?? They appear smoother than the one u posted before.
    Still there will be slight jagging, you cannot draw very straight line between points of different height.

    Also try setting the pen width to 2-5 if you want. This will give more smooth lines, as more no of pixels. Also take care of bounding rect if pen width is too much.
    Last edited by aamer4yu; 30th January 2007 at 06:31.

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.