January 10, 2011

Aurelien Aurelien
Lab Rat
3 posts

[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

  1. import os
  2. import sys
  3. import tempfile
  4.  
  5. from PyQt4.QtCore import *
  6. from PyQt4.QtGui import *
  7.  
  8. #QLineEdit   (to replace below)
  9. class MyWebView(QTextBrowser):
  10.     def dragEnterEvent(self, e):
  11.         e.accept()
  12.         print "Dragged"
  13.  
  14.     def dropEvent(self, e):
  15.         print "Dropped"    # This is never printed when using QTextBrowser
  16.         print e.mimeData().text()
  17.  
  18. class MyWindow(QWidget):
  19.     def __init__(self, *args):
  20.         QWidget.__init__(self, *args)
  21.  
  22.         layout = QVBoxLayout(self)
  23.        
  24.         view1 = MyWebView()
  25.         layout.addWidget(view1)
  26.         view1.setAcceptDrops(True)
  27.  
  28.         view2 = QLineEdit()
  29.         layout.addWidget(view2)
  30.         view2.setAcceptDrops(True)
  31.  
  32.         QObject.connect(view2, SIGNAL("dragEnterEvent(QDragEnterEvent)"), self.dragReceived)
  33.         QObject.connect(view2, SIGNAL("dropEvent(QDropEvent)"), self.dropReceived)
  34.  
  35.     def dragReceived(self, e):
  36.         e.accept()
  37.         print "Connect Dragged"
  38.  
  39.     def dropReceived(self, e):
  40.         print "Connect Dropped"
  41.  
  42. def main():
  43.     app = QApplication(sys.argv)
  44.  
  45.     w = MyWindow()
  46.    
  47.     w.show()
  48.     sys.exit(app.exec_())
  49.  
  50. if __name__ == '__main__':
  51.     main()

Thanks for your help
Aurelien

4 replies

January 10, 2011

Franzk Franzk
Lab Rat
840 posts

Is acceptDrops set to true?

 Signature 

“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

January 10, 2011

Aurelien Aurelien
Lab Rat
3 posts

Yes in line 26 in the init function

January 10, 2011

Franzk Franzk
Lab Rat
840 posts

It’s basically a QTextEdit. Maybe the QTextEdit drag ‘n’ drop [doc.trolltech.com] documentation helps.

 Signature 

“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

January 10, 2011

Aurelien Aurelien
Lab Rat
3 posts

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.

  1.     def dragMoveEvent(self, inEvent):
  2.         """
  3.        Need to accept DragMove to catch drop for TextBrowser
  4.        """
  5.         inEvent.accept()

Thanks all

 
  ‹‹ PySide: Python for Qt version 1.0.0~beta3 "salmiak" released      Problem installing PySide with easy_install ››

You must log in to post a reply. Not a member yet? Register here!