Results 1 to 20 of 37

Thread: moving one graphical item over other item...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default moving one graphical item over other item...

    Hi,
    I want to make the green circle move through the big grey circle as if it is a progress bar...please see the link ..
    i am not at all sure how to accomplish this,for my part i think it would be somehow through QPainterPath,its what i think....any help
    Attached Images Attached Images
    • File Type: gif 1.GIF (3.4 KB, 44 views)

  2. #2
    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: moving one graphical item over other item...

    With what exactly do you have a problem? This seems a simple task...

  3. #3
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: moving one graphical item over other item...

    basically i have implemented the picture in the previous attachment such that the green circle is expressed by QPainterPath..while the grey circle has been drawn in paint() function of QGraphics Item...
    i'll post the code here , i am not sure if doing things this way ,i would be able to move the green circle on grey circle...here is the code

    Qt Code:
    1. ProgressWidget::ProgressWidget()
    2. {
    3. r1.setRect(85,-13.5,30,30);
    4. path.addEllipse(r1);
    5. }
    6.  
    7. void ProgressWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    8. QWidget *widget)
    9. {
    10. painter->save();
    11. painter->setPen(QPen(QBrush(Qt::gray,Qt::SolidPattern),30,Qt::SolidLine,Qt::FlatCap,Qt::MiterJoin));
    12. painter->drawEllipse(0, 0, 200, 200);
    13. painter->restore();
    14. painter->fillPath( path, QBrush( Qt::green, Qt::SolidPattern ) );
    15. }
    16. QRectF ProgressWidget::boundingRect() const
    17. {
    18. qreal penWidth = 150;
    19. return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,20 + penWidth / 2, 20 + penWidth / 2);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class ProgressWidget:public QGraphicsItem
    2. {
    3. public:
    4. QRectF r1;
    5.  
    6.  
    7.  
    8. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    9. QWidget *widget);
    10. QRectF boundingRect() const;
    11.  
    12. ProgressWidget();
    13. ~ProgressWidget();
    14. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: moving one graphical item over other item...

    well, how about adding a slot advance() to that widget, connect it to a QTimer;

    advance() increments an internal counter (the angle of your circle) and calls update();

    base the drawing (the position) of the circle on that angle.

    HTH

  5. #5
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: moving one graphical item over other item...

    your idea seems to be acceptable..but the problem here is the moving of the green circle in grey circle ...the question is how can i move it...for now i just want to move it...

  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: moving one graphical item over other item...

    QGraphicsItem::setPos() is used to move items around. But basically I don't know why you are doing this inside graphics view, unless of course this is a requirement. I would implement a simple custom widget and draw the circles in the paint event based on the current progress.

  7. #7
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: moving one graphical item over other item...

    Ok,so what i understand is that,that this function of drawing two circles and moving can be easily done if i do it by QWidget instead of QGraphicsItem...
    do i understand correctly...
    but in my case i have a QGraphicsSCene in which i want to include the item ,thatswhy i consider it as an item..
    also is there any chance of implementing the movement using the code in my first post...i have used a QPainterPath() there..i mean to ask that am i going in the wrong direction by using it...or i can also implement it that way...Thanks
    Last edited by salmanmanekia; 1st July 2008 at 09:57.

  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: moving one graphical item over other item...

    Quote Originally Posted by salmanmanekia View Post
    Ok,so what i understand is that,that this function of drawing two circles and moving can be easily done if i do it by QWidget instead of QGraphicsItem...
    You can do it using graphics view as well, but in this case it only complicates things without giving any benefit.

    If you need graphics view, then implement your progress bar as a single item - this way it's the same as you would implement it as a widget.

  9. #9
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: moving one graphical item over other item...

    If you need graphics view, then implement your progress bar as a single item - this way it's the same as you would implement it as a widget
    actually i am implementing it as a single item as you can see in my code in first post ...or do u mean to say that i should consider both the green circle and grey circle as seperate items instead of using something like QPainterPath for the green circle ..
    also
    You can do it using graphics view as well, but in this case it only complicates things without giving any benefit.
    can you please explain how it makes things easy if i use QWidget because i thought this hierarchy is good like QGraphicsView->Scene->Item....

  10. #10
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: moving one graphical item over other item...

    no, you're not implementing it as a single item.
    You are implementing an item that does half the work by itself but contains another item.

    Wysota suggested to not use an item in your item, but do all the work in your item in paint().

    Just reimplement paint() -you'd have to do that regardless whether you're implementing a QWidget or a QGraphicsViewItem- and paint both your big circle and the small one in it.
    The small ones position depends on some progress measure.

    HTH

  11. #11
    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: moving one graphical item over other item...

    Oh, one more thing - get rid of the painter path, you don't need it. Simply draw two or three ellipses.

  12. #12
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: moving one graphical item over other item...

    no, you're not implementing it as a single item.
    can you please put more light on it,because i still think that there is only one item..
    also correct me if i am wrong that you are suggesting me to just simply draw two ellipses as
    QGraphicsEllipseItem and then find some mechanism to move one ellipse over another in the paint() function...
    The small ones position depends on some progress measure.
    also any guide on 'SOME PROGRESS MEASURE '..
    Thanks alot

Similar Threads

  1. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  2. Moving an item in QGraphicsScene
    By prosass in forum Newbie
    Replies: 4
    Last Post: 28th March 2007, 14:21

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.