[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):
    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()
        view2 = QLineEdit()
        QObject.connect(view2, SIGNAL("dragEnterEvent(QDragEnterEvent)"), self.dragReceived)
        QObject.connect(view2, SIGNAL("dropEvent(QDropEvent)"), self.dropReceived)
    def dragReceived(self, e):
        print "Connect Dragged"
    def dropReceived(self, e):
        print "Connect Dropped"

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

    w = MyWindow()

    if name == 'main':

    Thanks for your help

  • 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)

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

Log in to reply

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