Results 1 to 4 of 4

Thread: how to use dragMove()..

  1. #1
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default how to use dragMove()..

    hi,

    i had written an application in which i use drag and drop concept.in that use dragCopy for dragging the items. but i need move the item itself instead of copying. i tried using dragMove() function instead of dragCopy(). but i could not move the item. the following is my code:

    Qt Code:
    1. class drag : public QLabel
    2. {
    3. public:
    4. drag(QWidget* parent=0, const char* name=0);
    5. protected:
    6. void mousePressEvent( QMouseEvent * );
    7. };
    8.  
    9. drag::drag(QWidget * parent,const char * name):
    10. QLabel(parent,name)
    11. {
    12.  
    13. }
    14.  
    15. void drag::mousePressEvent( QMouseEvent * a )
    16. {
    17. QDragObject *drobj;
    18. if ( pixmap() )
    19. drobj = new QImageDrag( pixmap()->convertToImage(), this );
    20. drobj->dragMove();
    21. }
    To copy to clipboard, switch view to plain text mode 

    is this correct ? if no means can anyone correct me ?

    thanks in advance,

  2. #2
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: how to use dragMove()..

    try this:
    Qt Code:
    1. void drag::mousePressEvent(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::LeftButton)
    4. {
    5. QMimeData *mimeData = new QMimeData;
    6. mimeData->setText("myDrageLabel");
    7. QDrag *drag = new QDrag(this);
    8. drag->setMimeData(mimeData);
    9. Qt::DropAction dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
    10. if (dropAction == Qt::MoveAction)
    11. {
    12. close();
    13. update();
    14. }
    15. }
    16. }
    17. in your base class, in which you are using drag object (say myWidget)
    18. myWidget::myWidget(QWidget *parent) :
    19. QWidget(parent)
    20. {
    21. setAcceptDrops(true);
    22. ...
    23. }
    24. void myWidget::dragEnterEvent(QDragEnterEvent *event)
    25. {
    26. if (event->mimeData()->hasText())
    27. {
    28. event->acceptProposedAction();
    29. }
    30. else
    31. {
    32. event->ignore();
    33. }
    34. }
    35. void myWidget::dropEvent(QDropEvent *event)
    36. {
    37. if (event->mimeData()->hasText())
    38. {
    39. //QString plainText = event->mimeData()->text();
    40. QPoint dropPos = event->pos();
    41. QDragObject *drobj;
    42. if ( pixmap() )
    43. drobj = new QImageDrag( pixmap()->convertToImage(), this );
    44. drobj->move(dropPos);
    45.  
    46. drobj->show();
    47.  
    48. if (children().contains(event->source()))
    49. {
    50. event->setDropAction(Qt::MoveAction);
    51. event->accept();
    52. }
    53. else
    54. {
    55. event->acceptProposedAction();
    56. }
    57. }
    58. else
    59. {
    60. event->ignore();
    61. }
    62. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to use dragMove()..

    Hi Rajesh,

    thanks for your code. but unfortunately i'm using older version of qt(qt-3.3.5). in that i dont have QMimeData etc. but let me go through the code and try to adapt with teh functions in qt-3.3.5. but i think the problem in my code was that i used QPixmap::load() function to display the image. and then drag this image to the drop widget. this is my code:

    Qt Code:
    1. static void addStuff( QWidget * parent, bool image, bool secret = FALSE )
    2. {
    3. QVBoxLayout * tll = new QVBoxLayout( parent);
    4. if(image)
    5. {
    6. drag *drg = new drag(parent);
    7. drag *drg1 = new drag(parent);
    8. tll->addWidget( drg );
    9. tll->addWidget( drg1 );
    10. QPixmap stuff;
    11. QPixmap stuff1;
    12. if(stuff.load( "trolltech.bmp" )); ----i think this caused my image not to be moved---
    13. itemname[0]=1;
    14. stuff1.load( "butterfly.png" );
    15. drg->setPixmap( stuff );
    16. drg1->setPixmap( stuff1 );
    17. }
    18. else
    19. {
    20. drop *drp= new drop(parent);
    21. drop *drp1= new drop(parent);
    22. tll->addWidget( drp );
    23. tll->addWidget( drp1 );
    24. drp->setText("Drop the items");
    25. drp->setFont(QFont("Helvetica",12));
    26. drp1->setText("Drag and Drop");
    27. drp1->setFont(QFont("Helvetica",12));
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    .

    thinking of using QIconview. any suggestions ?

  4. #4
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: how to use dragMove()..

    I dont know about Qpixmap::load. but in your previous code you creating a new object and not deleteing old object. so it will create a copy of new widget.
    mainly whenever you create a new widget then delete or close old widget.

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.