Results 1 to 18 of 18

Thread: How to move an item on a GraphicsScene

  1. #1
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to move an item on a GraphicsScene

    Hi,
    II'm trying to move a GraphicsItem on a GraphicsScene using moveBy()Function but nothing moves on the screen , may someone help me to achieve this ?
    Thanks in advance
    here is the Programmcode i tried to write :
    -------
    Qt Code:
    1. class Container: public QGraphicsRectItem
    2. {
    3. public:
    4. Container(QGraphicsItem* parent=0, intx, int y);
    5. };
    6. Container::Container(QGraphicsItem* parent, intx=0, int y=0): QGraphicsRectItem(parent)
    7. {QColor color (0,0,100);
    8. QPen p(color);
    9. p.setWidth(2);
    10. QBrush brush(QColor(0,160,0));
    11. setPen(p);
    12. setBrush(brush);
    13. setRect()x,y,100,60;
    14. show();
    15. }
    To copy to clipboard, switch view to plain text mode 
    --------
    Qt Code:
    1. #include"Container.h"
    2. class Scene: public QGraphicsScene
    3. {
    4. public:
    5. Scene(QWidget* parent=0);
    6. Container*container;
    7. };
    8. Scene::Scene(QWidget* parent):QGraphicsScene(parent)
    9. {
    10. QBrush brush(QColor(180,180,220), Qt::Dense4Pattern);
    11. setBackgroundBrush(brush);
    12. }
    To copy to clipboard, switch view to plain text mode 
    --------
    mainGui class
    Qt Code:
    1. #include"include/gui/mainGui.h"
    2. #include"include/gui/Scene.h"
    3. #include"include/gui/container.h"
    4. class MainGui:public QWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. MainGui(QWidget*parent=0);
    9. private:
    10. };
    11. MainGui::MainGui(QWidget *parent):QWidget(parent)
    12. {
    13. scene=newScene(this);
    14. Container*c1= new Container(0,0,0);
    15. // I want the item to move step by step after any 1000 ms on the Screen
    16. QMoveEvent *event;
    17. int x=event->pos().x();
    18. int y=event->pos().y();
    19. c1->moveBy((qreal)x,(qreal)y);
    20. startTimer(1000);
    21. c1->addItem(c1);
    22. scene->addLine(-5, -200, -5, 200);
    23. scene->addLine(105, -200,105, 200);
    24. scene->advance();
    25. QGraphicsView *view =new QGraphicsView (scene);
    26. QGridLayout *layout= new QGridLayout();
    27. layout->addWidget(view);
    28. setLayou(layout);
    29. view->show();
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 3rd June 2008 at 22:41. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to move an item on a GraphicsScene

    Quote Originally Posted by Holy View Post
    // I want the item to move step by step after any 1000 ms on the Screen
    QMoveEvent *event;
    int x=event->pos().x();
    int y=event->pos().y();
    c1->moveBy((qreal)x,(qreal)y);
    It won't work --- you take x and y values from an uninitialized object.


    Quote Originally Posted by Holy View Post
    startTimer(1000);
    When you use startTimer(), you have to reimplement QObject::timerEvent() and put the code that you want to run every second there.

    You need something like:
    Qt Code:
    1. void MainGui::timerEvent( QTimerEvent *e )
    2. {
    3. Q_UNUSED( e);
    4. scene->advance();
    5. }
    To copy to clipboard, switch view to plain text mode 
    You can also consider using QTimer instead of startTimer()/timerEvent().

    To make scene->advance() work, you have to reimplement QGraphicsItem::advance() in your Container class or use QGraphicsItemAnimation.

    P.S. Please, don't post the same question twice in two different threads.

  3. #3
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post Re: How to move an item on a GraphicsScene

    hi,
    Sorry for the posts and thanks for your answer.
    i tried to do like you said namely:
    ------ MainGui.cpp
    Qt Code:
    1. MainGui::MainGui(QWidget *parent): QWidget(parent)
    2. {
    3. ...
    4. }
    5. void MainGui::timerEvent(QTimerEvent *e)
    6. {
    7. Q_UNUSED(e);
    8.  
    9. anim.setItem(c1);
    10.  
    11. for(int i=0; i<200; i++)
    12. {
    13. anim.yTranslationAt(i/200.0);
    14.  
    15. }
    16. scene->advance();
    17. update();
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    but it doesn't work.
    I also tried like this:


    Qt Code:
    1. MainGui::MainGui(QWidget *parent): QWidget(parent)
    2. {
    3.  
    4. //QWidget *myWidget= new QWidget();
    5.  
    6. scene = new Scene(this);
    7.  
    8. Container *c1 = new Container(0, 0, 0);
    9.  
    10. QTimeLine *timeline = new QTimeLine(5000);
    11. anim.setItem(c1);
    12. for(qreal i=0.0;i<200.0;i++)
    13. {
    14. anim.setTranslationAt(i/200.0,i,i);
    15. anim.yTranslationAt(i/200.0);
    16.  
    17. }
    18.  
    19. anim.setTimeLine(timeline);
    20. timeline->setUpdateInterval(1000/3);
    21. timeline->setLoopCount(0);
    22. timeline->setDuration(5000);
    23. timeline->start();
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    the item doesn't move as well, please can you write it in the right way so that it can work?
    thanks in advance
    Last edited by jpn; 17th June 2008 at 15:15. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to move an item on a GraphicsScene

    You create the animation object on the stack and it gets destroyed as soon as the constructor ends. Make it a member variable.

  5. #5
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to move an item on a GraphicsScene

    I did it, but there's no change, nothing moves.

    can someone post me an example that works please?
    what i want is to move the item upper in the vertical direction.
    thanks for help.

  6. #6
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to move an item on a GraphicsScene

    it works now! thanks very much for the last idea!
    but there's another problem, the item is moving down. How can i get it moving up first and then moving down and the movement should be inside the lines (i mean the item should remain inside the lines when moving) .
    thanks for your help.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to move an item on a GraphicsScene

    Quote Originally Posted by Holy View Post
    but there's another problem, the item is moving down. How can i get it moving up first and then moving down and the movement should be inside the lines
    The movement is controlled by the animation object:
    Qt Code:
    1. for( int i = 0; i < 50; ++i ) {
    2. anim.setTranslationAt( i / 100.0, 0, i );
    3. }
    4. for( int i = 50; i < 100; ++i ) {
    5. anim.setTranslationAt( i / 100.0, 0, 50 - i );
    6. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to move an item on a GraphicsScene

    thanks for your reply;
    i have another problem , while inserting items in the scene all items are added at the same time but not one after another like i want them to be added, i've tried to controll this adding action of the scene by controlling the QTimeLine of Container :

    something like this:
    the variable QTimeLine timeline is declared public in the class container; i use it to controll the animation of the containers.
    Qt Code:
    1. void MainGui::addnextContainer(int i, QGraphicsScene *scene1)
    2. {
    3. for(int i=0;i<containerNum; i++)
    4. {
    5. Container*[i];
    6. scene1->addItem(c[i]);
    7. c[i]->timeline->start();
    8. c[i+1]->timeline->setPaused(true);
    9. l_items(c[i]);
    10. c[i]->timeline->stop();
    11. scene1->addItem(c[i+1]);
    12. c[i+1]->timeline->start();
    13. l_items()
    14.  
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    puting the items in a range in the scene

    Qt Code:
    1. QList<QGraphicsItem*>MainGui:: l_items(QGraphicsItem *item1)
    2. {
    3. QList<QGraphicsItem*>listing=scene->items();
    4. listing.clear();
    5. for(int i=0; i<listing.size();i++)
    6. {
    7. listing.append(item1);
    8.  
    9. }
    10. return listing;
    11. }
    To copy to clipboard, switch view to plain text mode 
    and i changed the MainGui.cpp
    Qt Code:
    1. ...
    2. Scene scene= new Scene(this);
    3. addnextContainer(5, scene)
    4. ...
    To copy to clipboard, switch view to plain text mode 
    can someone take a look at this code and tell me please what i'm doing wrong please? now i even cannot see any item anymore.
    thanks in advance
    Last edited by jpn; 17th June 2008 at 15:16. Reason: missing [code] tags

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to move an item on a GraphicsScene

    What is l_items() method supposed to do? Now it just returns an empty list.

  10. #10
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to move an item on a GraphicsScene

    apology for the late reply, i did something else as i didn't get the solution of this problem.
    back to your question.
    the l_items() method is supposed to list the containers objects which are added to the scene. Can you also help me by controlling the addItem() method so that the containers should be added one by one and not all at the same time?
    thanks in advance.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to move an item on a GraphicsScene

    Quote Originally Posted by Holy View Post
    the l_items() method is supposed to list the containers objects which are added to the scene.
    Lines 5--9 are never executed because you clear the list in line 4.

    Quote Originally Posted by Holy View Post
    Can you also help me by controlling the addItem() method so that the containers should be added one by one and not all at the same time?
    What do you mean by "one by one"? Do you want to have some kind of animation?

  12. #12
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to move an item on a GraphicsScene

    the items are already animated and it works very well, but what i need now is to get control over the addItem() method of scene, so that when i add n items in the scene for instance, i should get them added one after another and not as a group of n added of course as animation.
    i also tried to remove listing.clear() completely but it doesn't do anything different.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to move an item on a GraphicsScene

    Quote Originally Posted by Holy View Post
    the items are already animated and it works very well, but what i need now is to get control over the addItem() method of scene, so that when i add n items in the scene for instance, i should get them added one after another and not as a group of n added of course as animation.
    Would it be enough if there was some delay between calls to addItem()?

    Quote Originally Posted by Holy View Post
    i also tried to remove listing.clear() completely but it doesn't do anything different.
    Better rewrite that method from scratch.

  14. #14
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to move an item on a GraphicsScene

    From Guru:Would it be enough if there was some delay between calls to addItem()?

    i don't understand what you mean , can you please send me the example code to compile myself and see what you exactly mean with"some delay between calls to addItem()"

    I have another question concerning Qt4.3 Network. There is an example named :"blockingFortuneclient" , i've tried to run that with eclipse on window(vista) but i cannot run it succesfully. wenn i try to input a port number it breaks, i mean it doesn't enable to input the port number.Can someone tell me please what is the problem.

    Another question is : can i implement a middleware of RFID-System in the same way as this example to receive Data from a RFID-Reader?
    or does someone know how to implement an RFID-receiver with Qt ? i will be very thankfull if someone send me an example code.
    thanks for your reply.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to move an item on a GraphicsScene

    Quote Originally Posted by Holy View Post
    From Guru:Would it be enough if there was some delay between calls to addItem()?
    As a result user would see that one item appears on the scene, after a while another one appears, then another and so on. Is that what you want by writing "one after another"?

    Quote Originally Posted by Holy View Post
    what you exactly mean with"some delay between calls to addItem()
    In Qt applications you usually add delay with QTimer.

    Quote Originally Posted by Holy View Post
    I have another question concerning Qt4.3 Network. There is an example named :"blockingFortuneclient" , i've tried to run that with eclipse on window(vista) but i cannot run it succesfully. wenn i try to input a port number it breaks, i mean it doesn't enable to input the port number.Can someone tell me please what is the problem.
    What "breaks" mean in exactly this case? Does the application crash or it starts properly but you can't enter anything into line edits?

    Quote Originally Posted by Holy View Post
    Another question is : can i implement a middleware of RFID-System in the same way as this example to receive Data from a RFID-Reader?
    Everything depends on the RFID reader and its drivers. Some readers have USB or RS-232C interfaces and for these you don't need any networking code.

    Next time please ask unrelated questions in new threads.

  16. #16
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to move an item on a GraphicsScene

    Guru
    As a result user would see that one item appears on the scene, after a while another one appears, then another and so on. Is that what you want by writing "one after another"?
    Holy:
    it's exactly what i mean .
    Guru:
    In Qt applications you usually add delay with QTimer.
    Holy:
    Isn't it possible with QTimeLine? can i get an example please?
    Guru:
    What "breaks" mean in exactly this case? Does the application crash or it starts properly but you can't enter anything into line edits?
    Holy: I don't know what the problem was, i've just closed Eclipse and now it's alright,it's working again.
    Thanks for your answer.

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to move an item on a GraphicsScene

    Quote Originally Posted by Holy View Post
    Isn't it possible with QTimeLine? can i get an example please?
    No, because you can't control visibility with QTimeLine.

    You need:
    • a list of items that must be added to the scene,
    • a slot which adds an item from the list to the scene,
    • QTimer which fires that slot until the list is empty.


    P.S. Please, use the [quote] tags.

  18. #18
    Join Date
    May 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to move an item on a GraphicsScene

    Quote Originally Posted by jacek View Post
    No, because you can't control visibility with QTimeLine.

    You need:
    • a list of items that must be added to the scene,
    • a slot which adds an item from the list to the scene,
    • QTimer which fires that slot until the list is empty.


    P.S. Please, use the [ quote ] tags.
    [QUOTE=Holy;76800].
    I'll try like u say. Thanks
    Last edited by jacek; 26th July 2008 at 00:21. Reason: fixed bbcode

Similar Threads

  1. Move and resize with layout
    By waediowa in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2008, 08:16
  2. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  3. Replies: 1
    Last Post: 19th April 2007, 22:23
  4. how to display full tree item name on mouse move ?
    By rajesh in forum Qt Programming
    Replies: 5
    Last Post: 15th November 2006, 08:41
  5. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 01:34

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.