[Moved] Drop Event on QTextBrowser ? (PyQt)



  • 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



  • Is acceptDrops set to true?



  • Yes in line 26 in the init function



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



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.