Page 1 of 2 12 LastLast
Results 1 to 20 of 37

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

  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

  13. #13
    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...

    I got the impression that the green circle was an item. Seems it is not. Sorry for the confusion.

    I (we) merely suggested that you just draw 2 ellipses in paint().
    Something like that (untried)

    Qt Code:
    1. void ProgressWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    2. QWidget *widget)
    3. {
    4. painter->save();
    5. painter->setPen(QPen(QBrush(Qt::gray,Qt::SolidPattern),
    6. 30,Qt::SolidLine,Qt::FlatCap,Qt::MiterJoin));
    7. painter->drawEllipse(0, 0, 200, 200);
    8.  
    9. // draw your progress marker (the circle) over it
    10. // rotate/translate painter so you don't have to do trigonometry
    11. painter->translate(100,100); // move origin to middle
    12. painter->rotate(...); // depending on your progress
    13. painter->translate(0,80); // move origin into your ellipse
    14. painter->setPen(QPen(QBrush(Qt::green,Qt::SolidPattern),
    15. 3,Qt::SolidLine,Qt::FlatCap,Qt::MiterJoin));
    16.  
    17. painter->drawEllipse(0, 0, 20,20);
    18. painter->restore();
    19. }
    To copy to clipboard, switch view to plain text mode 

    'SOME PROGRESS MEASURE':
    • a counter that increases with your 'progress'
    • I would suggest to use an int, ranged from 0-359; interpreted as the angle (see: rotate);
    • with every advance, you increase (mod 360) this angle
    • that way repeatedly advancing will make your little circle move round the ellipse

    HTH

  14. #14
    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...

    Thanks ..it seems you have left me with deciding how to rotate...Thanks AGAIN

  15. #15
    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...

    not how to rotate, just to what angle.
    QPainter::rotate etc do all the hard work for you. You just have to provide an angle.

    See the AnalogClock example for the usage of rotate.

  16. #16
    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...

    To be honest with you, i am realy surprised that few lines could do this thing..i thought i had to write some lengthy code...any ways the good part is that i did understand now how things works....
    i had some question popping regarding QPainter and related to that ,so there they are
    1,Initially as you know i was trying to use QGraphicsPathItem...can you tell me why PathItem is not suitable in my case and where should it be used ?
    2,Also can you tell what difference does it make with save() and restore() method..
    and last but not the least
    ..Thanks

  17. #17
    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...

    1,Initially as you know i was trying to use QGraphicsPathItem...can you tell me why PathItem is not suitable in my case and where should it be used ?
    Don't know. Probably it is not efficient or, for your simple need, overy complicated.

    well
    2,Also can you tell what difference does it make with save() and restore() method..
    I have to guess a bit here...

    basically you are passed a QPainter. Calling rotate() -amongst other things- you modifiy it.
    So, either the code calling you, has to restore the painter to a know good state, or you have to. Otherwise the painting of other parts of the widget might use your (inappropriate) modifications to the QPainter.

    If it is your responsibility, it is only done when necessary.

  18. #18
    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...

    Thanks ..but i think my problem are not over yet...
    i am trying to move green circle this way but it shows green circle on view everywhere else then the grey circle ....


    Qt Code:
    1. painter->setPen(QPen(QBrush(Qt::gray,Qt::SolidPattern),30,Qt::SolidLine,Qt::FlatCap,Qt::MiterJoin));
    2. painter->drawEllipse(0, 0, 200, 200);
    3. painter->setPen(QPen(QBrush(Qt::green,Qt::SolidPattern),15,Qt::SolidLine,Qt::FlatCap,Qt::MiterJoin));
    4.  
    5. for(int i = 30; i< 120; i=i+30)
    6. {
    7. painter->translate(dx,dy);
    8. painter->rotate(i);
    9. painter->translate(0,91);
    10. painter->drawEllipse(0,0,15,15);
    11. }
    To copy to clipboard, switch view to plain text mode 

    also i want the previous green circle to be removed as the new green circle draws itself to the next position ...and it should look like a progress ..as if green circle revolves around grey in a couple of second...i hope you understand..

  19. #19
    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...

    You have to redraw the whole widget when the progress value changes and not draw all of the values at once

  20. #20
    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...

    You have to redraw the whole widget when the progress value changes and not draw all of the values at once
    aah..i am confused i have to draw the WHOLE widget and also not draw ALL values...whats this supposed to mean

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.