Results 1 to 7 of 7

Thread: QTextEdit drag and drop

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default QTextEdit drag and drop

    Hey there trolltechers,

    I'm using a QTextEdit dynaically allocated inside a widget:
    - When the mouse enters: I create and place it.
    - When the mouse leaves: I delete it.

    Now I have glitches with the following:
    - Contextual menu makes the app crash since the mouse leaves the parent widget:
    solved it with : mTextBrowser->setContextMenuPolicy(Qt::NoContextMenu);

    But :
    - Drag and dropping makes the app crashes since the mouse leaves the parent too when dragging something.


    I tried with deleteLater() and that doesn't solve a thing.
    Any way to disable drag and dropping on a QTextEdit? Am I doing something wrong by creating deleting in the enter/leave event?

    Thanks.

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

    Default Re: QTextEdit drag and drop

    What exactly are you creating and deleting? The text edit widget? What is your purpose?

  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextEdit drag and drop

    Ok,

    I have a custom "bubble widget" like the one in an iChat conversation.

    When hoovering the widget with the mouse I'm replacing painted text by a QTextEdit to get the "copy", "open links" (etc) functionnalities.

    Only problem is when drag and dropping a block of text out of the bubble it crashes because the leave event deletes my QTextEdit.

    So my question is :
    - Is there a way to disable Drag and drop on a QTextEdit.
    - Or do you have a better approach for doing it.

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

    Default Re: QTextEdit drag and drop

    Try using a QLabel and use QLabel::setTextInteractionFlags() with Qt::TextBrowserInteraction. If it doesn't give you what you want, use QTextBrowser or QTextEdit instead but don't delete it when leaving its borders - instead remove its frame so that it looks like an ordinary text. Or you can even have the frame removed all the time. QTextEdit has QFrame as its ancesstor so you can manipulate its frame shape.

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

    bunjee (7th February 2008)

  6. #5
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextEdit drag and drop

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3.  
    4. /* virtual */ void ZeParagraphWidget::enterEvent(QEvent * event)
    5. {
    6. if (mTextBrowser)
    7. {
    8. mTextBrowser->document()->deleteLater();
    9. mTextBrowser->deleteLater();
    10. mTextBrowser = NULL;
    11. }
    12.  
    13. mTextBrowser = new QTextBrowser(this);
    14. mTextBrowser->setDocument(mTextDocument->clone());
    15. mTextBrowser->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    16. mTextBrowser->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    17. mTextBrowser->setStyle(ZeStyle::get());
    18.  
    19. mTextBrowser->setOpenExternalLinks(true);
    20. mTextBrowser->setContextMenuPolicy(Qt::NoContextMenu);
    21.  
    22. mTextBrowser->move(-1, -1);
    23. mTextBrowser->resize(mTextDocument->idealWidth() + 2, height() + 2);
    24. mTextBrowser->show();
    25. update();
    26. }
    To copy to clipboard, switch view to plain text mode 

    That stuff is working. Instead of tweaking the QFrame I set my own style.
    Only problem is : when entered the newly created TextBrowser never gets deleted.

    I guess would need some kind of isActive() flag for the QTextBrowser in order to choose wether I can delete it or not in the leaveEvent.

    Thanks man.
    Last edited by bunjee; 7th February 2008 at 13:22.

  7. #6
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextEdit drag and drop

    Can someone offer advice on how to disable drag and drop in a QTextEdit? I
    would like to handle it manually.

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

    Default Re: QTextEdit drag and drop

    Quote Originally Posted by bunjee View Post
    Only problem is : when entered the newly created TextBrowser never gets deleted.
    Why should it get deleted? Create it when you create the widget and leave it there, regardless of entering or leaving the area. Just use QTextBrowser instead of whatever you used to use there.

    Quote Originally Posted by bunjee View Post
    Can someone offer advice on how to disable drag and drop in a QTextEdit? I
    would like to handle it manually.
    Looks like the only thing you want is copy & paste (+drag and drop which is really copy&paste too). Why don't you forget about using a text edit and implement copy & paste functionality for a completely custom widget?

Similar Threads

  1. Drag & drop with model/view (Qt4)
    By yogeshm02 in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2011, 20:36
  2. Problem with a cursor during Drag and Drop
    By mtrpoland in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2007, 15:46
  3. Strange behavior with Drag and Drop (not always drops)
    By Mona Shokrof in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2007, 18:00
  4. Replies: 7
    Last Post: 8th September 2006, 16:19
  5. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41

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.