Results 1 to 20 of 21

Thread: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    Hi!

    I have a tree view which display different object. Some of the objects are groups of objects. I want to take a object and drag it and release it into a group. If the object can be put into the object it is inserted, otherwise the object is replaced where the dragging started.

    I have not found much about the drag and drop thing so I'm not sure about which components to use. I can drag a object, but it is displayed as a big floating icon and I want just a line (when between two objects) showing where in the tree the object will be put if I drop it, when the dragged object is dragged over a object it should highlight it or something showing that the dragged object will be placed in this one if dropped... the dragging and dropping is only allowed in the same tree view.

    I hope you get the picture....

    Any tips on where I should start look for this? Any good sample code or functions I can use?


    /pir

  2. #2
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    I was thinking that I mayble should do the line that indicates the dragging by drawing on top of the tree view. But The problem is that I need some kind of transparent layer to put on top on my tree view that allows me to paint on it. I suppose that it has something to do with a pixmap. But how do I put to pixmap on top over the tree view as a layer?

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

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    You don't need any layer. You can paint in paintEvent directly on the widget. dragEnterEvent will probably help you (should help you mark where to draw the line).

  4. #4
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    I've managed to paint on top of the tree now by, like you said, call the paintEvent(). But the thing is that I want the line to follow the mouse and show the drop target.

    The problem is that I don't understand how dragEnterEvent can help me... it seems to only start when I start a drag and I need to know how the mouse is changing during the dragging process.

    I've tried dragMoveEvent(). The problem there is that I need to repaint the widget while the object is dragging and it only happens when the dragging has stopped. I tried to trigger the paintEvent by calling repaint() from inside the dragMouseEvent(). I wonder if it is the floating icon that is preventing me from repainting the widget... but I can't find out how to turn it off.

    thanks for the tip!
    /pir

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

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    use mouseMove to update cursor position. dragEnter will just help you to know that you are dragging an object.

  6. #6
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    The problem is that I can't get the treeView to redraw the widget so the line is animated. What happens is that I start dragging and after I have stopped dragging by released the mouse the line is drawn. There is something that prevents the paintEvent to redraw the treeView while I'm dragging...

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

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    Did you call update() from mouseMoveEvent?

  8. The following user says thank you to wysota for this useful post:

    pir (20th May 2006)

  9. #8
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    No, I don't think I've tried that. I called repaint from dragMoveEvent().
    I should try mouseMoveEvent()...

  10. #9
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    Ok, I can't make it work.

    This is what I have so far... it is mostly taken from the docs, with some deactivated. I hope that isn't the problem...

    The problem is that when I start a drag operation the mouseMoveEvent() isn't called during the draging. To be able to react to the dragging I need to implement the dragMoveEvent(). The problem is that if I do this, it doesn't seem like paintEvent is called during the dragging. It is called after the dragging has stopped.

    Here is the events I have implemented and their placement in the class. The tree view itself lies in a window.

    Qt Code:
    1. class Outliner: public QTreeeView {
    2.  
    3. protected:
    4.  
    5. /** mouseMoveEvent() */
    6. void mouseMoveEvent ( QMouseEvent *event )
    7. {
    8. if ( !(event->buttons() & Qt::LeftButton) )
    9. return;
    10.  
    11. if ( (event->pos() - m_qDragStartPosition).manhattanLength() <
    12. Application::startDragDistance() )
    13. return;
    14.  
    15. QDrag *drag = new QDrag ( this );
    16.  
    17. QMimeData *mimeData = new QMimeData;
    18.  
    19. //mimeData->setData ( mimeType, data );
    20.  
    21. drag->setMimeData ( mimeData );
    22.  
    23. Qt::DropAction dropAction = drag->start ( Qt::CopyAction | Qt::MoveAction );
    24.  
    25. repaint ( );
    26.  
    27. }// mouseMoveEvent(QMouseEvent*)
    28.  
    29. /** mousePressEvent() */
    30. void mousePressEvent ( QMouseEvent *event )
    31. {
    32. QTreeView::mousePressEvent ( event );
    33.  
    34. if ( event->button() == Qt::LeftButton )
    35. m_qDragStartPosition = event->pos ( );
    36.  
    37. }// mousePressEvent(QMouseEvent*)
    38.  
    39. /** paintEvent() */
    40. void paintEvent ( QPaintEvent *event )
    41. {
    42. QTreeView::paintEvent ( event );
    43.  
    44. QPainter painter ( viewport );
    45.  
    46. int x, y, w, h;
    47. m_rDropSite.getRect ( &x, &y, &w, &h );
    48. painter.drawLine ( 0, y, 192, y );
    49.  
    50. }// paintEvent(QPaintEvent*)
    51.  
    52. };// class Outliner
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    Ok, I have managed to make the tree view to redraw itself during a dragMoveEvent().
    Right now I'm not using the mouseMoveEvent() and mousePressEvent() functions I posted above, instead I use dragMoveEvent() and the posted paintEvent().

    dragMouseEvent looks like this:
    Qt Code:
    1. void Outliner::dragMoveEvent ( QDragMoveEvent *event )
    2. {
    3. // a QRect member variable telling where the drag event is currently localted, used by paintEvent().
    4. m_rDropSite = event->answerRect ( );
    5.  
    6. // TreeModel is my custom QAbstractItemModel
    7. TreeModel *tree = static_cast<TreeModel*> ( model() );
    8.  
    9. // look below for this one
    10. tree->emit_layoutChanged ( );
    11.  
    12. }// dragMoveEvent(QDragMoveEvent*)
    To copy to clipboard, switch view to plain text mode 

    This is the thing I feel is a bit bizarr... It seems to be the only way to update
    the tree view during a drag operation. This lies in the custom model, class TreeModel, for the view.
    Is it ok to do like this?

    Qt Code:
    1. /** Public member function in custom model, class TreeModel. The only thing this does is
    2.  * emit the signal layoutChanged(), forcing the tree view to execute paintEvent.
    3.  */
    4. void TreeModel::emit_layoutChanged ( )
    5. {
    6. emit layoutChanged ( );
    7. }// emit_layoutChanged()
    To copy to clipboard, switch view to plain text mode 

    anyway, thanks for the tips.
    /pir
    Last edited by pir; 20th May 2006 at 14:33.

  12. #11
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    Hi!

    Now I'm stuck again. Thought that it would be best to post this question in this thread, since it puts this question in context.

    Now I want the drawn line to either be drawn between the items or in the middle of one. The reason for this is to make it easier for the user to see if the drop site will place the dragged item between two items or inside one. To do this I need to know the coordinates for the rendered items.


    So the question is:
    How do I get a describtion of the geometrity for a item in my QTreeView? That is, where in the view the item specified is draw defined in view pixel coordinates.

    hope somebody knows how to do this or if it's possible...
    pir

Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  2. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  3. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 09:16
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.