Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37

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

  1. #21
    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 animate the ellipse over the other ellipse by drawing one frame during every redraw cycle and not all frames during one redraw cycle. When you update the progress, call update() and your widget will redraw itself with the new value.

  2. #22
    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 animate the ellipse over the other ellipse by drawing one frame during every redraw cycle and not all frames during one redraw cycle
    sorry but may be it would sound absurd,but do you mean that the number of times i have to draw the green circle is the number of times i have to implement the paint function...it somehow doesnt make sense to me..but this is what i am able to understand..

  3. #23
    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 need one paint() function.
    This function is called when the CURRENT state of your widget/item is to be drawn.
    I.e. you implement this function to paint your circle at the one position that corresponds to your current angle/progress value.


    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. painter->translate(dx,dy);
    5. // draw one circle at the position that is stored in your widget
    6. // i.e. your widget knows its state (in numbers): just visualize it
    7. painter->rotate(yourCurrentAngleAsStoredInSomeWidgetMemberVariable);
    8. painter->translate(0,91);
    9. painter->drawEllipse(0,0,15,15);
    To copy to clipboard, switch view to plain text mode 

    When progress advances, you increase your "yourCurrentAngleAsStoredInSomeWidgetMemberVariabl e" variable and call update().
    When Qt sometime later causes a repaint, paint() gets called and you just access the current value of that variable.
    This results in an animation.

    I.e. you do not animate inside paint().
    paint() paints snapshots, no movement or animation whatsoever.
    Animation results from the repeated calls (of update() -> paint()) as a sequence of such snapshots.

    HTH

  4. #24
    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...

    When progress advances, you increase your "yourCurrentAngleAsStoredInSomeWidgetMemberVar iabl e" variable and call update().
    does this has to be done by signal/slots in my paint() function ?

    Animation results from the repeated calls (of update() -> paint()) as a sequence of such snapshots.
    where should these call be make and are you sure about update()->paint()..because it doesnt work

  5. #25
    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, as i wrote in an earlier posting:

    add a slot to your widget/item, call it advance() -or whatever, let's assume you called it advance()-
    Qt Code:
    1. YourItem::advance()
    2. {
    3. angle +=10;
    4. angle %=360;
    5. update();
    6. }
    To copy to clipboard, switch view to plain text mode 
    (and don't forget the Q_OBJECT macro in your class declaration!)

    somewhere else:
    Qt Code:
    1. QTimer timer;
    2. yourItem->connect(&timer, SINGAL(timeout()), SLOT(advance()));
    3. timer.start(25);
    To copy to clipboard, switch view to plain text mode 

    (Of course your timer must not be/go out of scope, otherwise the animation stops.
    Perhaps it is best to declare it as QTimer* in your widget and allocate it with new.)

  6. #26
    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...

    i have tried to implement the thing this way ,but it wont work..
    now i have two questions when i debug the code compiler says that
    'warning: Object::connect: No such slot QObject::advance()'

    and most importantly am i doing right thing concept wise..

    Qt Code:
    1. ProgressWidget::ProgressWidget()
    2. {
    3. r1.setRect(85,-13.5,30,30);
    4. dx = 100;
    5. dy = 100;
    6. timerProg = new QTimer();
    7. connect(timerProg,SIGNAL(timeout()),this,SLOT(advance()));
    8. timerProg->start(1000);
    9. }
    10.  
    11. void ProgressWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    12. QWidget *widget)
    13. {
    14. //painter->save();
    15. painter->setPen(QPen(QBrush(Qt::gray,Qt::SolidPattern),30,Qt::SolidLine,Qt::FlatCap,Qt::MiterJoin));
    16. painter->drawEllipse(0, 0, 200, 200);
    17. painter->setPen(QPen(QBrush(Qt::green,Qt::SolidPattern),15,Qt::SolidLine,Qt::FlatCap,Qt::MiterJoin));
    18. painter->translate(dx,dy);
    19. painter->rotate(rotation);
    20. painter->translate(0,91);
    21. painter->drawEllipse(0,0,15,15);
    22. }
    23. QRectF ProgressWidget::boundingRect() const
    24. {
    25. qreal penWidth = 150;
    26. return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,20 + penWidth / 2, 20 + penWidth / 2);
    27. }
    28. void ProgressWidget::advance()
    29. {
    30. rotation = rotation + 30;
    31. this->update();
    32. }
    33. ProgressWidget::~ProgressWidget()
    34. {
    35. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QObject>
    2. #include <QtGui/QWidget>
    3. #include <QTimer>
    4.  
    5. #include <QGraphicsItem>
    6. #include <QPainter>
    7. #include <QPainterPath>
    8.  
    9. class ProgressWidget:public QGraphicsItem,public QObject
    10. {
    11. public:
    12. QRectF r1;
    13.  
    14. qreal dx ;
    15. qreal dy ;
    16.  
    17. QTimer *timerProg;
    18.  
    19. int rotation;
    20.  
    21. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    22. QWidget *widget);
    23. QRectF boundingRect() const;
    24.  
    25. ProgressWidget();
    26. ~ProgressWidget();
    27.  
    28. public slots:
    29. void advance();
    30. };
    31.  
    32. #endif
    To copy to clipboard, switch view to plain text mode 

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

    also surprisingly if i add Q_OBJECT macro it throws a compile time error..and says 'connect undeclared'

  8. #28
    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...

    sorry for the last post..it was a mistake from my side ..anyways ..the green bar moves around the grey circle but if i only minimized and then maximized my output screen...it issnt moving on the screen if i dont minimize it...to simplify it doesnt move in normal conditions

  9. #29
    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...

    The code looks ok (perhaps the 1000ms you pass in start is a bit large).

    If you provide (attachment) a self-contained compilable example, we can try to help you with the minimize/maximize issue.

  10. #30
    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...

    i have tried it with lesser value ..but still the same result...in the docs i found this statement..
    This function does not cause an immediate paint; instead it schedules a paint request that is processed by QGraphicsView after control reaches the event loop
    ..my class hiererachy is something like i am calling from a main function a class named training
    such as
    class training: public QGraphicsView..
    from this class i call an instance of
    class scene such as
    class scene: public QGraphicsScene
    and after that in hierarchy comes the class item
    class item: public QGraphicsItem..
    and all the work i am doing for now is done in item class..so again coming to the docs i think this line has soemthing to do with the situation i am facing...

  11. #31
    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...

    Put debug statements (qDebug() << __PRETTY_FUNCTION__ inside the slot and the paint() function... that way you can see if these get called as often as expected.

  12. #32
    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...

    i am using eclipse integration with qt ..and it shows that the paint() and advance slots are called consecetively...
    i think this problem is due to something else ,may be as i posted in my previous post...

  13. #33
    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 provide (attachment) a self-contained compilable example,
    what do you mean ?,the code or .exe or something else....i didnt get it

  14. #34
    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, compilable kind of implies source code, right ?

    I am Linux based, so your exe would not help me. And I hardly could debug/modify it.
    If you can post a (minimal) compilable example (as a .zip, or .tar.gz file) I could try it out and help.

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

    Hi..
    I have attached all the source ,header and .pro file..i hope you would be able to figure out something...just one small correction,please remove TrainingUI.qrc from the .pro file
    Attached Files Attached Files

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

    The error is: your bounding rect is not correct.
    Your code does try to repaint, but the incorrect bounding rect prevents the relevant area to be redrawn.

    Return something like QRectF(0,0,300,300) (which errs, too, but on the 'safe' side). And you see will something moving.

    Please read up in the docs on QGraphicsItem::boudingRect and perhaps QGraphicsItem::shape.

    HTH

  17. #37
    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 worked...u deserve a great Thanks ..for all the way helping me into this...and answering sometime il/logical questions

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.