Results 1 to 6 of 6

Thread: Drag and Drop Crush when second time drag

  1. #1
    Join Date
    Jan 2019
    Posts
    3
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Drag and Drop Crush when second time drag

    Hi forum again me pls help me.

    i just want to do that;

    ive got two label and i want to drag first label text to drop second label and code worked fine(i dont know why :))). but i want to do this in second time it crushed.
    i think i should to clear mimedata but how?? or what i should to do?

    here is my . h file
    Qt Code:
    1. public:
    2. explicit Dialog(QWidget *parent = nullptr);
    3. ~Dialog();
    4. void mousePressEvent(QMouseEvent *event) ;
    5. QDrag *drag = new QDrag(this);
    6. QMimeData *mimeData = new QMimeData;
    7. QString labeltext;
    8.  
    9.  
    10. private:
    11. Ui::Dialog *ui;
    12.  
    13. protected:
    14. bool eventFilter(QObject *obj, QEvent *event);
    To copy to clipboard, switch view to plain text mode 

    and dialog .cpp
    Qt Code:
    1. #include <QEvent>
    2. #include <QDragEnterEvent>
    3. #include <QDragMoveEvent>
    4. #include <QDropEvent>
    5. #include <QDebug>
    6. #include <QMouseEvent>
    7. #include <QDrag>
    8. #include <QMimeData>
    9.  
    10. Dialog::Dialog(QWidget *parent) :
    11. QDialog(parent),
    12. ui(new Ui::Dialog)
    13. {
    14. ui->setupUi(this);
    15. ui->label_2->installEventFilter(this);
    16. ui->label_2->setAcceptDrops(true);
    17. }
    18.  
    19. Dialog::~Dialog()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void Dialog::mousePressEvent(QMouseEvent *event)
    25. {
    26. if (event->button() == Qt::LeftButton
    27. && ui->label->geometry().contains(event->pos()))
    28. {
    29.  
    30. labeltext = ui->label->text();
    31.  
    32. mimeData->setText(labeltext);
    33. drag->setMimeData(mimeData);
    34. drag->start();
    35. }
    36. }
    37.  
    38. bool Dialog::eventFilter(QObject *obj, QEvent *event)
    39. {
    40. if (event->type() == QEvent::DragEnter)
    41. {
    42. QDragEnterEvent *tDragEnterEvent = static_cast<QDragEnterEvent *>(event);
    43. tDragEnterEvent->acceptProposedAction();
    44.  
    45. return true;
    46. }
    47.  
    48. else if (event->type() == QEvent::DragMove)
    49. {
    50. QDragMoveEvent *tDragMoveEvent = static_cast<QDragMoveEvent *>(event);
    51. tDragMoveEvent->acceptProposedAction();
    52.  
    53. return true;
    54. }
    55.  
    56.  
    57. else if (event->type() == QEvent::Drop)
    58. {
    59. QDropEvent *tDropEvent = static_cast<QDropEvent *>(event);
    60. tDropEvent->acceptProposedAction();
    61.  
    62. ui->label_2->setText(mimeData->text());
    63.  
    64.  
    65. return true;
    66. }
    67.  
    68. return QObject::eventFilter(obj, event);
    69. }
    To copy to clipboard, switch view to plain text mode 
    please help mee pleeeeaseeeee :((

    (and pls i dont want to use subclass for this)
    Last edited by anda_skoa; 23rd February 2019 at 12:18. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop Crush when second time drag

    A crash is usually a good source to find what is wrong, e.g. by running the program in debugger and checking which call causes the crash.
    Especially if you have a reliable way of triggering the crash.

    Educated guess: the second time calls methods on a now invalid QDrag pointer.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2019
    Posts
    3
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop Crush when second time drag

    All my respect sir,

    What am i doing please show me the way

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop Crush when second time drag

    1) Compile the program in debug mode
    2) Run the program in the debugger
    3) Check where it crashes

    Debugging a crash is the most fundamental thing to learn.

    Telling you the cause and/or solution for this specific crash does not help you in the long run

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,976
    Thanks
    232
    Thanked 627 Times in 617 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Drag and Drop Crush when second time drag

    Educated guess: the second time calls methods on a now invalid QDrag pointer.
    And probably an invalid QMimeData pointer as well, since QDrag::setMimeData() takes ownership and if the QDrag instance is destroyed, so will be the QMimeData instance.

    drag->start();
    I can't find a QDrag:: start() method, so where did this come from, and how did you get the code to compile in the first place?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop Crush when second time drag

    Quote Originally Posted by d_stranz View Post
    And probably an invalid QMimeData pointer as well, since QDrag::setMimeData() takes ownership and if the QDrag instance is destroyed, so will be the QMimeData instance.
    Yep

    Quote Originally Posted by d_stranz View Post
    I can't find a QDrag:: start() method, so where did this come from, and how did you get the code to compile in the first place?
    Deprecated and thus not part of the API documentation.
    But still in the header.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    d_stranz (26th February 2019)

Similar Threads

  1. Replies: 2
    Last Post: 30th January 2014, 06:46
  2. Replies: 0
    Last Post: 7th January 2012, 15:20
  3. Replies: 2
    Last Post: 13th October 2010, 21:51
  4. Replies: 3
    Last Post: 10th June 2010, 15:13
  5. Replies: 0
    Last Post: 4th May 2010, 10:24

Tags for this Thread

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.