Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Moved] Drop Event on QTextBrowser ? (PyQt)
QtWS25 Last Chance

[Moved] Drop Event on QTextBrowser ? (PyQt)

Scheduled Pinned Locked Moved Language Bindings
5 Posts 2 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aurelien.collard
    wrote on last edited by
    #1

    Hi Guys,

    First, I hope I didn't miss any thread already answering this question.
    My problem is fairly simple. I can't get a drop event to work correctly for a QTextBrowser. It work just fine with a QLineEdit.

    Please check code below (and replace QTextBrowser by QLineEdit to see what I expect)

    #!/usr/bin/env python
    @
    import os
    import sys
    import tempfile

    from PyQt4.QtCore import *
    from PyQt4.QtGui import *

    #QLineEdit (to replace below)
    class MyWebView(QTextBrowser):
    def dragEnterEvent(self, e):
    e.accept()
    print "Dragged"

    def dropEvent(self, e):
        print "Dropped"    # This is never printed when using QTextBrowser
        print e.mimeData().text()
    

    class MyWindow(QWidget):
    def init(self, *args):
    QWidget.init(self, *args)

        layout = QVBoxLayout(self)
        
        view1 = MyWebView()
        layout.addWidget(view1)
        view1.setAcceptDrops(True)
    
        view2 = QLineEdit()
        layout.addWidget(view2)
        view2.setAcceptDrops(True)
    
        QObject.connect(view2, SIGNAL("dragEnterEvent(QDragEnterEvent)"), self.dragReceived)
        QObject.connect(view2, SIGNAL("dropEvent(QDropEvent)"), self.dropReceived)
    
    def dragReceived(self, e):
        e.accept()
        print "Connect Dragged"
    
    def dropReceived(self, e):
        print "Connect Dropped"
    

    def main():
    app = QApplication(sys.argv)

    w = MyWindow()
    
    w.show()
    sys.exit(app.exec_())
    

    if name == 'main':
    main()@

    Thanks for your help
    Aurelien

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      Is acceptDrops set to true?

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aurelien.collard
        wrote on last edited by
        #3

        Yes in line 26 in the init function

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          It's basically a QTextEdit. Maybe the "QTextEdit drag 'n' drop":http://doc.trolltech.com/latest/qtextedit.html#drag-and-drop documentation helps.

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aurelien.collard
            wrote on last edited by
            #5

            Thanks for your help.

            I managed to find my problem. I actually need to accept dragMoveEvent which is the type of Drag action that emit a TextBrowser when you drag and drop a file from your desktop onto your QTextBrowser.

            For information (action 2)
            http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qt.html#DropAction-enum

            Just by adding the following code it work well.
            @
            def dragMoveEvent(self, inEvent):
            """
            Need to accept DragMove to catch drop for TextBrowser
            """
            inEvent.accept()
            @
            Thanks all

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved