qt - QListView not accepting drops in QMainWindow with Python PyQt4 -


i'm not being able make qlistwidget accept dropped files.

i've used qt designer make qmainwindow , qlistwidget.

how can solve problem?

this main.py

import sys import os    pyqt4 import qtcore, qtgui pythonapp import ui_mainwindow  class mainwindow(ui_mainwindow):     def __init__(self):         super(mainwindow, self).__init__()          listview = ui_mainwindow.listview         listview.setacceptdrops(true)         listview.seticonsize(qtcore.qsize(72, 72))         self.connect(listview, qtcore.signal("dropped"), self.picturedropped)         self.setcentralwidget(listview)      def picturedropped(self, l):     listview = ui_mainwindow.listview         url in l:             if os.path.exists(url):                 print(url)                                 icon = qtgui.qicon(url)                 pixmap = icon.pixmap(72, 72)                                 icon = qtgui.qicon(pixmap)                 item = qtgui.qlistwidgetitem(url, listview)                 item.seticon(icon)                         item.setstatustip(url)                   def dragenterevent(self, event):         if event.mimedata().hasurls:             event.accept()         else:             event.ignore()      def dragmoveevent(self, event):         if event.mimedata().hasurls:             event.setdropaction(qtcore.qt.copyaction)             event.accept()         else:             event.ignore()      def dropevent(self, event):         if event.mimedata().hasurls:             event.setdropaction(qtcore.qt.copyaction)             event.accept()             links = []             url in event.mimedata().urls():                 links.append(str(url.tolocalfile()))             self.emit(qtcore.signal("dropped"), links)         else:             event.ignore()      def main():         app = qtgui.qapplication(sys.argv)         mainwindow = qtgui.qmainwindow()         ui = ui_mainwindow()         ui.setupui(mainwindow)          mainwindow.show()         sys.exit(app.exec_())      if __name__ == '__main__':         main() 

this pythonapp.pyfile:

from pyqt4 import qtcore, qtgui  try:     _fromutf8 = qtcore.qstring.fromutf8 except attributeerror:     _fromutf8 = lambda s: s  class ui_mainwindow(object):     def setupui(self, mainwindow):         mainwindow.setobjectname(_fromutf8("mainwindow"))         mainwindow.resize(800, 600)         self.centralwidget = qtgui.qwidget(mainwindow)         self.centralwidget.setacceptdrops(true)         self.centralwidget.setobjectname(_fromutf8("centralwidget"))         self.listview = qtgui.qlistview(self.centralwidget)         self.listview.setgeometry(qtcore.qrect(100, 60, 501, 331))         self.listview.setacceptdrops(true)         self.listview.setdragdropmode(qtgui.qabstractitemview.dragdrop)         self.listview.setdefaultdropaction(qtcore.qt.copyaction)         self.listview.setobjectname(_fromutf8("listview"))         mainwindow.setcentralwidget(self.centralwidget)         self.menubar = qtgui.qmenubar(mainwindow)         self.menubar.setgeometry(qtcore.qrect(0, 0, 800, 22))         self.menubar.setobjectname(_fromutf8("menubar"))         mainwindow.setmenubar(self.menubar)         self.statusbar = qtgui.qstatusbar(mainwindow)         self.statusbar.setobjectname(_fromutf8("statusbar"))         mainwindow.setstatusbar(self.statusbar)          self.retranslateui(mainwindow)         qtcore.qmetaobject.connectslotsbyname(mainwindow)      def retranslateui(self, mainwindow):         mainwindow.setwindowtitle(qtgui.qapplication.translate("mainwindow", "mainwindow", none, qtgui.qapplication.unicodeutf8)) 

see how example works can modify code:

#!/usr/bin/env python #-*- coding:utf-8 -*-  #--------- # import #--------- import sys, os  pyqt4 import qtgui, qtcore  #--------- # define #--------- class mylistwidget(qtgui.qlistwidget):     dropped = qtcore.pyqtsignal(list)      def __init__(self, type, parent=none):         super(mylistwidget, self).__init__(parent)          self.setacceptdrops(true)      def dragenterevent(self, event):         if event.mimedata().hasurls:             event.accept()          else:             event.ignore()      def dragmoveevent(self, event):         if event.mimedata().hasurls:             event.setdropaction(qtcore.qt.copyaction)             event.accept()          else:             event.ignore()      def dropevent(self, event):         if event.mimedata().hasurls:             event.setdropaction(qtcore.qt.copyaction)             event.accept()              filepaths = [                 str(url.tolocalfile())                 url in event.mimedata().urls()             ]              self.dropped.emit(filepaths)          else:             event.ignore()  class mywindow(qtgui.qwidget):     def __init__(self, parent=none):         super(mywindow, self).__init__(parent)          self.listwidgetfiles = mylistwidget(self)         self.listwidgetfiles.dropped.connect(self.on_listwidgetfiles_dropped)          self.layoutvertical = qtgui.qvboxlayout(self)         self.layoutvertical.addwidget(self.listwidgetfiles)      @qtcore.pyqtslot(list)     def on_listwidgetfiles_dropped(self, filepaths):         filepath in filepaths:             if os.path.exists(filepath):                 qtgui.qlistwidgetitem(filepath, self.listwidgetfiles)  #--------- # main #--------- if __name__ == "__main__":             app = qtgui.qapplication(sys.argv)     app.setapplicationname('mywindow')      main = mywindow()     main.resize(333, 111)     main.show()      sys.exit(app.exec_()) 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -