Results 1 to 18 of 18

Thread: Moving of QGraphicsItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Question Moving of QGraphicsItem

    Hi.

    I am making a timeline widget based on Qt's amazing Graphics View framework.
    Now as you an see on the picture attached, I have three lines where I am putting QGraphicsItems.

    Now, I have made them movable with the ItemIsMovable flag, but I only want each item to be movable on the X axis (from left to right). Currently, you can move them in every direction.

    What is the best way to achive this?

    Many thanks,
    Erlend Graff
    Attached Images Attached Images

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Moving of QGraphicsItem

    The best way is to loose the ItemIsMovable flag and implement your own item moving functionality ( using the item mouse events in conjunction with scene coordinates ).

    Then, all you have to do is restrict the Y position of the item and alow only X to be modified.

    Regards

  3. #3
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Cool Re: Moving of QGraphicsItem

    There's a better way. In your item, reimplement the itemChange() virtual function. Handle the ItemPositionChange case to fixate one of your axises. You can keep QGraphicsItem's default move implementation, and still only allow moving along one axis.

    Qt Code:
    1. QVariant MyItem::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. if (change == ItemPositionChange)
    4. return QPointF(pos().x(), value.toPointF().y());
    5. return QGraphicsItem::ItemChange(change, value);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Neat? ;-)
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  4. The following 3 users say thank you to Bitto for this useful post:

    Erlendhg (3rd May 2007), Havard (3rd May 2007), marcel (3rd May 2007)

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Moving of QGraphicsItem

    Neat? ;-)
    Indeed it is

  6. #5
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Talking Re: Moving of QGraphicsItem

    Wow.
    That was a great solution. Got to keep so much as possible of Qt's fantastic native implementation.

    Thanks

  7. #6
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Question Re: Moving of QGraphicsItem

    Hi.
    Now I am here again to annoy you with another question.

    This is what I want to achieve:
    When I move an item in my timeline widget, I want to have a tooltip showing the current position where the item starts, and where it stops.

    This, I first wrote like this:

    Qt Code:
    1. void eventItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. QGraphicsItem::mouseMoveEvent(event);
    4. QToolTip::showText(event->screenPos(), QString("This item goes from position %1 to %2").arg(startOfItem).arg(startOfItem+lengthOfItem));
    5. }
    To copy to clipboard, switch view to plain text mode 

    where startOfItem and lengthOfItem is variables in the items class, telling the start and length of the item. These are updated in itemChange(), therefore the call to QGraphicsItem::mouseMoveEvent(event); before the tooltip.

    But earlier, when I moved my items around, the program could suddenly freeze, and nothing would then happen.

    This, I guess, may have been a problem that I have fixed somehow, 'cause later on now, I can't re-create it. (I am still quite n00b in Qt :P)

    But my real question is:
    Is there a better way to acheive this?
    If possible, I would like to see the tooltip less "flashy", if you understand what I mean.

    Many thanks,
    Erlend Graff
    Attached Images Attached Images
    Last edited by Erlendhg; 7th May 2007 at 15:23. Reason: Forgot to add picture.

  8. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Moving of QGraphicsItem

    Why a tooltip? Why not use a "floating" widget that you custom-draw( you could make it look better than a tooltip - transparent, etc ).

    To keep the widget glued to the graphics item you can call QWidget::move, setGeometry, etc. You have the mouse position, anyway.

    It really should be easy - just fill it, draw some text and set transparency.

    regards

  9. #8
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Moving of QGraphicsItem

    Have you seen this blog? http://labs.trolltech.com/blogs/2006...raphics-items/

    Like Marcel says, you probably want to do something like what this example shows. When you move the cursor over certain parts of the item, your "tooltips" could hover up just like the circular indicators show (in this example it's not even a separate item). If you want it to be resolution independent, you could also set QGraphicsItem::ItemIgnoresTransformations on the tooltip-ish item.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  10. #9
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default Re: Moving of QGraphicsItem

    Sorry, I didn't get that.
    But I think I might have made me a bit unclear.
    What I want, is that when I click and drag items in my timeline, a tooltip will always show the current start and stop position of the item, until the mouse button is released.

    That is what my code does, except every time I move the mouse (while dragging an item), the tooltip is redrawn, and this makes it "flashy".

    I want to make it as simple as possible, so I thought a tooltip would not be too hard, but it seems to be more difficult than expected.

  11. #10
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default Re: Moving of QGraphicsItem

    Hi again.
    I am here with another problem.
    I have stripped my timeline code, to show you the problem.

    Attached is the code and a video (very crappy) trying to explain what happens.

    When I add an item (click outside the QGraphicsView, and click "Q"), slides it to the beginning of the view, resizes it down quite a bit, and deletes it (with "W"), it doesn't disappear, but is resized up.

    I can't understand why this happens, so I hope you guys can help me solve it.

    (I'm pretty sure it must be something I have done wrong, because the process of the program also uses very much CPU!)

    If it is relevant: I am using Qt 4.2.2

    Many thanks,
    Erlend Graff
    Attached Files Attached Files

  12. #11
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Moving of QGraphicsItem

    I can't reproduce your bug, it works fine for me. However, you have forgotten to delete the eventItems after you remove them. This might be the source of your problem.

    Try
    Qt Code:
    1. void EventView::removeItem(eventItem *item)
    2. {
    3. int index = getIndexFromItem(item);
    4.  
    5. eventScene->removeItem(eventList[index]);
    6. delete eventList.takeAt(index);
    7. }
    To copy to clipboard, switch view to plain text mode 

  13. #12
    Join Date
    Nov 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Moving of QGraphicsItem

    Quote Originally Posted by Bitto View Post
    There's a better way. In your item, reimplement the itemChange() virtual function. Handle the ItemPositionChange case to fixate one of your axises. You can keep QGraphicsItem's default move implementation, and still only allow moving along one axis.

    Qt Code:
    1. QVariant MyItem::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. if (change == ItemPositionChange)
    4. return QPointF(pos().x(), value.toPointF().y());
    5. return QGraphicsItem::ItemChange(change, value);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Neat? ;-)
    Awesome!!!!
    Last edited by Matthew.J; 5th November 2013 at 15:14. Reason: i forgot to set flag...

Similar Threads

  1. Disable default tab behaviour for a QGraphicsItem
    By nmather in forum Qt Programming
    Replies: 3
    Last Post: 13th December 2017, 10:30
  2. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  3. QGraphicsItem with QSizeGrip
    By elsheikhmh in forum Newbie
    Replies: 7
    Last Post: 19th November 2009, 18:53
  4. Deleting a scene from QGraphicsItem mouseEvent
    By JonathanForQT4 in forum Qt Programming
    Replies: 5
    Last Post: 10th April 2007, 11:27
  5. why pushbutton moving??
    By Shuchi Agrawal in forum Qt Tools
    Replies: 7
    Last Post: 19th January 2007, 17:17

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.