Results 1 to 18 of 18

Thread: Moving of QGraphicsItem

  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 16: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 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Unhappy Re: Moving of QGraphicsItem

    Hi.
    Good spotted, but when I update the code to delete the item, and do the same thing as I did to "produce" the bug, I get this:

    Unhandled exception at 0x6554f2b1 (QtGuid4.dll) in EventEditor.exe: 0xC0000005: Access violation reading location 0xfeeeff26.

    And the program breaks here:

    Qt Code:
    1. class QGraphicsSceneFindItemBspTreeVisitor : public QGraphicsSceneBspTreeVisitor
    2. {
    3. public:
    4. QList<QGraphicsItem *> *foundItems;
    5.  
    6. void visit(QList<QGraphicsItem *> *items)
    7. {
    8. for (int i = 0; i < items->size(); ++i) {
    9. QGraphicsItem *item = items->at(i);
    10. if (!item->d_func()->itemDiscovered && item->isVisible()) { <--- Here it stops
    11. item->d_func()->itemDiscovered = 1;
    12. foundItems->prepend(item);
    13. }
    14. }
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 

    Weird that you couldn't reproduce the problem.
    Did you add one more item, slide the first to the very beginning, and resize it down a lot, before you pressed "w"?

    Are you using Qt 4.2.2 as well?
    Could this be a bug in Qt?

  14. #13
    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'm using Qt 4.2.0 open source on windows. I did exactly what you did in the movie clip and it worked just like it should. Your exception confirms my belief that the item isn't removed properly. I can't tell you why, but you might simplify your code by removing the repeated calls to itemToIndex and indexToItem.
    Deleting objects in an event handler can be hairy. Try letting your items be subclasses of QObject as well and call QObject::deleteLater(). That might prevent the crash, but doesn't answer why the items aren't removed as they should.
    Good Luck!

  15. #14
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default Re: Moving of QGraphicsItem

    Can QGraphicsItems be subclassed by QObject?
    I tried that earlier, but then my program wouldn't compile.
    It didn't look like it fancyed QObject...

    I have seen the problem of deleting an item in an event handler, but I thought that was only for the items itself's event handlers. These items are deleted in the mainwindow's event handler.

    Thanks anyway,
    Erlend Graff

  16. #15
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Moving of QGraphicsItem

    Quote Originally Posted by Erlendhg View Post
    Can QGraphicsItems be subclassed by QObject?
    I tried that earlier, but then my program wouldn't compile.
    It didn't look like it fancyed QObject...
    Yes it can be done using multiple inheritance as follows. I guess someone had already asked this before. Probably you can find more info by forum search
    Anyway here is quick one

    Qt Code:
    1. class Item : public QObject, public QGraphicsItem
    2. {
    3. Q_OBJECT
    4. public:
    5. ..
    6.  
    7. public slots:
    8. ..
    9. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by Gopala Krishna; 15th May 2007 at 19:44. Reason: reformatted to look better
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  17. #16
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Unhappy Re: Moving of QGraphicsItem

    That doesn't seem to help either
    Even when I subclass QObject and use QObject::deleteLater() instead of deleting the item, I get the same bug.

  18. #17
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Talking Re: Moving of QGraphicsItem

    Ok, finally I found the solution of my quite annoying problem.
    It was me that hadn't read the documentation carefully enoughed, so I had missed that QGraphicsItem:repareGeometryChange() had to be called whenever I resize my timeline items :P

    After adding that, everything seems to work just fine
    Just wanted to tell, in case someone experiences similar problems:
    Remember to call QGraphicsItem:repareGeometryChange() before you change the boundingRect() of a QGraphicsItem

  19. The following user says thank you to Erlendhg for this useful post:

    Havard (27th June 2007)

  20. #18
    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 16: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, 11:30
  2. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 11:31
  3. QGraphicsItem with QSizeGrip
    By elsheikhmh in forum Newbie
    Replies: 7
    Last Post: 19th November 2009, 19:53
  4. Deleting a scene from QGraphicsItem mouseEvent
    By JonathanForQT4 in forum Qt Programming
    Replies: 5
    Last Post: 10th April 2007, 12:27
  5. why pushbutton moving??
    By Shuchi Agrawal in forum Qt Tools
    Replies: 7
    Last Post: 19th January 2007, 18: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.