Results 1 to 5 of 5

Thread: Drag and Drop QGraphicsTextItem

  1. #1
    Join Date
    Jul 2012
    Location
    Switzerland
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Drag and Drop QGraphicsTextItem

    Hello everyone,

    I'm quiet stuck and would really appreciate if someone could help me

    I have a derived class of QGraphicsScene and /view there are some Items which are derived from QGraphicsTextItem.
    If a mousePressEvent happens, I want to be able to move these Items to another position on the scene/view. But if these TextItems are set to editable, I want to set the cursor to the position, where the click happened.

    So I overwrote the mousePressEvent function of these TextItems to see if they are set editable or not. But now I don't really know how to do this, I tried to setFlag(QGraphicsItem::ItemIsMovable) but had no success... , I asked me if I should write an own function for Drag and Drop but I didn't found an example simple enough for my problem.

    Any Ideas of doing it otherwise are welcome

    thanks

    airglide

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Drag and Drop QGraphicsTextItem

    Set the ItemIsMovable when you create the item and don't override mousePressEvent handler for the item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2012
    Location
    Switzerland
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: Drag and Drop QGraphicsTextItem

    allright, but how can I set the cursor with a mouseclick?

    thanks

    airglide

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Drag and Drop QGraphicsTextItem

    It should work out of the box if you set proper flags for the item (Movable and Focusable).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2006
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop QGraphicsTextItem

    Hi everyone,

    As this thread is not so old, I try to post a question regarding the drag'n'drop with a QGraphicsTextItem (on Qt 4.7).

    I followed the Drag'n'drop Robot example to implement the drag'n'drop. But it seems that it does not work on QGraphicsTextItem. I subclassed the TextItem and implemented every useful method.

    If I change my parent class to another GraphicsItem such as EllipseItem, the drag'n'drop works, if it's derived from QGraphicsObject (as QGraphicsTextItem does), it's no working. Actually it's just the dropEvent part which is not working at all. The rest is perfectly working. But Qt won't let me to drop (I always have an icon forbidding me to do so) and the program never enters dropEvent in my subclass.

    Does someone have an idea regarding that problem?
    Qt Code:
    1. NodeTreeItem::NodeTreeItem(QGraphicsItem *parent, QGraphicsScene *scene)
    2. :QGraphicsTextItem(parent, scene)
    3. {
    4. //setFlag(QGraphicsItem::ItemIsSelectable);
    5. //setFlag(QGraphicsItem::ItemSendsGeometryChanges);
    6. setAcceptedMouseButtons(Qt::LeftButton);
    7. setAcceptDrops(true);
    8.  
    9. setCursor(Qt::OpenHandCursor);
    10. }
    11.  
    12. void NodeTreeItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
    13. {
    14. if(event->mimeData()->hasText())
    15. {
    16. event->setAccepted(true);
    17. update();
    18. dragOver = true;
    19. }
    20. else
    21. event->setAccepted(false);
    22. }
    23.  
    24. void NodeTreeItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
    25. {
    26. Q_UNUSED(event);
    27. dragOver = false;
    28. update();
    29. }
    30.  
    31. void NodeTreeItem::dropEvent(QGraphicsSceneDragDropEvent *event)
    32. {
    33. event->setAccepted(true);
    34. qDebug() << "I dropped it";
    35. dragOver = false;
    36. update();
    37. }
    38.  
    39. void NodeTreeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    40. {
    41. Q_UNUSED(event);
    42. setCursor(Qt::ClosedHandCursor);
    43. }
    44.  
    45. void NodeTreeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    46. {
    47. if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
    48. .length() < QApplication::startDragDistance()) {
    49. return;
    50. }
    51.  
    52. QDrag *drag = new QDrag(event->widget());
    53. QMimeData *mime = new QMimeData;
    54. mime->setText(this->toPlainText());
    55. drag->setMimeData(mime);
    56. drag->exec();
    57.  
    58. setCursor(Qt::OpenHandCursor);
    59. }
    60.  
    61. void NodeTreeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    62. {
    63. Q_UNUSED(event);
    64. setCursor(Qt::OpenHandCursor);
    65. }
    To copy to clipboard, switch view to plain text mode 

    Enabling ItemIsSelectable or ItemIsMovable does not solve the problem.

    Thanks in advance


    Added after 36 minutes:


    Hi,

    It's me again. I figured out the problem. Apparently you have to set the TextInteractionFlag (TextEditorInteraction) to allow the drop.

    So problem solved.

    Cheers
    Last edited by cocophotos; 22nd August 2012 at 15:35.

Similar Threads

  1. Replies: 0
    Last Post: 7th January 2012, 15:20
  2. Replies: 2
    Last Post: 13th October 2010, 21:51
  3. Replies: 3
    Last Post: 10th June 2010, 15:13
  4. Replies: 0
    Last Post: 4th May 2010, 10:24
  5. not drag and drop on a QGraphicsTextItem
    By jano_alex_es in forum Qt Programming
    Replies: 4
    Last Post: 19th October 2009, 08:49

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
  •  
Qt is a trademark of The Qt Company.