Results 1 to 5 of 5

Thread: Drag 'n Drop problem

  1. #1
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drag 'n Drop problem

    Hey guys,

    at first: thanks a lot for the good work, I fully support the step to build up a new site - looks awesome

    I posted this message a few hours ago at qtforum.org, when i hadn't read what was going on. So i hope this cross-post is ok

    I've got a strange problem with drag 'n drop in my app. It works if you drag the label for the first time, but afterwoods that "drag symbol"(little rect) just remains at the starting position, no matter where you move the mouse. The mouse cursor shows then that a drop isn't permitted (everywhere). The TemplateWidget then also doesn't get any dropevents anymore. I've got the same issue in another Dialog, but I use different Widgets there.

    Here's my code (using QT4), i wrote it after after having a look at the QT-Examples. QDragLabel is the Widget where the drag begins, it should end in a QTemplateWidget.

    Qt Code:
    1. void QDragLabel::mousePressEvent(QMouseEvent *event)
    2. {
    3. if (!(event->buttons() & Qt::LeftButton))
    4. return;
    5.  
    6. QDrag drag(this);
    7.  
    8. QMimeData *mimeData = new QMimeData;
    9.  
    10. mimeData->setText(m_text);
    11. drag.setMimeData(mimeData);
    12.  
    13. drag.start(Qt::CopyAction);
    14. event->accept();
    15. }
    16.  
    17. void QTemplateWidget::dragEnterEvent (QDragEnterEvent *event)
    18. {
    19. event->accept();
    20. }
    21.  
    22. void QTemplateWidget::dropEvent ( QDropEvent * event )
    23. {
    24. bool ok;
    25. QString text;
    26.  
    27. if(!event->mimeData()->hasText())
    28. {
    29. event->ignore();
    30. return;
    31. }
    32.  
    33. event->acceptProposedAction();
    34. if(event->mimeData()->text()=="Textfeld")
    35. {
    36. while(true)
    37. {
    38. text = QInputDialog::getText(this, "Bitte geben sie den Namen für das Textfeld an:",
    39. "Name:", QLineEdit::Normal,
    40. "", &ok);
    41. if (ok && !text.isEmpty())
    42. break;
    43. }
    44. QFontMetrics fontmetrics=QApplication::fontMetrics();
    45. QRect rect=fontmetrics.boundingRect(text);
    46. int x=event->pos().x()-rect.width()/2;
    47. int y=event->pos().y()-rect.height()/2;
    48. m_myLabelsX.append(x);
    49. m_myLabelsY.append(y);
    50.  
    51. m_myLabelsText.append(text);
    52. }
    53. update();
    54. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance,
    kiker99

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

    Default Re: Drag 'n Drop problem

    QDrag object should be created on heap and not on stack. (So QDrag *drag = ...)

    And you should probably use some more standard way to check if you should accept the drop
    Last edited by wysota; 16th January 2006 at 10:42.

  3. #3
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag 'n Drop problem

    Thanks a lot
    it works. Never thought about that could be the cause...
    You're also right with the checking issue, it would be best if the app would only support drags from QDragLabel. Perhaps I should create my own MimeType?

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

    Default Re: Drag 'n Drop problem

    I meant this:
    Qt Code:
    1. void QTemplateWidget::dragEnterEvent (QDragEnterEvent *event)
    2. {
    3. event->accept();
    4. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. if(!event->mimeData()->hasText())
    2. {
    3. event->ignore();
    4. return;
    5. }
    To copy to clipboard, switch view to plain text mode 

    The docs suggest:

    Qt Code:
    1. void Window::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. if (event->mimeData()->hasFormat("text/plain"))
    4. event->acceptProposedAction();
    5. }
    To copy to clipboard, switch view to plain text mode 

    You shouldn't accept every type that exists like you do.

  5. #5
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag 'n Drop problem

    yeah, thanks.

Similar Threads

  1. Drag and Drop problem
    By ScoOteR in forum Qt Programming
    Replies: 16
    Last Post: 19th May 2010, 15:57
  2. Problem with Drag Drop for QWT Graph
    By Ankitha Varsha in forum Qwt
    Replies: 2
    Last Post: 23rd January 2008, 06:21
  3. Problem wirh Drag and Drop for QWT Graph
    By Ankitha Varsha in forum Qwt
    Replies: 1
    Last Post: 22nd January 2008, 13:23
  4. Problem with a cursor during Drag and Drop
    By mtrpoland in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2007, 16:46
  5. Drag n Drop problem
    By ScoOteR in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2007, 11:52

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.