472,794 Members | 3,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

Drag and Drop with PyQt4

Hi,

I'm writing a program using Python 2.4 and PyQt4. The aim is to
implement drag and drop from filesystem and display a list of files
dragged on to the listWidget. This function will later become part of a
software for workflow management.

When I run the program, DragEnterEvent works as expected, but the
DropEvent does not seem to be working. I'm not an experienced Python
hacker, and am unable to trace out any problems with the source code.
Any and all help will be appreciated!

Cheers!
Harshad.

P.S.: The source code for the two files is given below:

------------------------------------------- zaraa.py
-------------------------------------------

from PyQt4 import QtCore, QtGui
from zaraamain import Ui_Dialog
import sys
from PyQt4 import *

class Zaraa(QtGui.QDialog, Ui_Dialog):
def __init__(self):
QtGui.QDialog.__init__(self)

# Set up the user interface from Designer.
self.setupUi(self)

# Enable Drag and Drop
self.listWidget.setAcceptDrops(True)

# Set up the handlers for the listWidget
self.listWidget.__class__.dragEnterEvent =
self.lwDragEnterEvent
self.listWidget.__class__.dropEvent = self.lwDropEvent

# Drag Enter Event handler
def lwDragEnterEvent(self, event):
print "DragEnter"
event.acceptProposedAction()

# ------------------------ BEGIN ------------------------
# The following event is not fired, or at least is not
# handled by this function - as is expected...

# Drag Drop Event Handler
def lwDropEvent(self, event):
print "DragDrop"

event.acceptProposedAction()

# we want to append only URLs to the list...
if event.mimeData().hasUrls() == True:
urllist = event.mimeData().urls()
for url in urllist:
self.listWidget.addItem(url.toLocalFile())
# ------------------------ END ------------------------

# Set up the application and execute it.
app = QtGui.QApplication(sys.argv)

window = Zaraa()
window.show()

sys.exit(app.exec_())

------------------------------------------- /zaraa.py
-------------------------------------------

------------------------------------------- zaraamain.py
-------------------------------------------
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'zaraamain.ui'
#
# Created: Sat Aug 26 00:00:10 2006
# by: PyQt4 UI code generator 4.0.1
#
# WARNING! All changes made in this file will be lost!

import sys
from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")

Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,249,30 0).size()).expandedTo(Dialog.minimumSizeHint()))

self.vboxlayout = QtGui.QVBoxLayout(Dialog)
self.vboxlayout.setMargin(9)
self.vboxlayout.setSpacing(6)
self.vboxlayout.setObjectName("vboxlayout")

self.listWidget = QtGui.QListWidget(Dialog)
self.listWidget.setObjectName("listWidget")
self.vboxlayout.addWidget(self.listWidget)

self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate ("Dialog",
"Zaraa", None, QtGui.QApplication.UnicodeUTF8))

------------------------------------------- /zaraamain.py
-------------------------------------------

Aug 25 '06 #1
2 6471
Harshad wrote:
When I run the program, DragEnterEvent works as expected, but the
DropEvent does not seem to be working. I'm not an experienced Python
hacker, and am unable to trace out any problems with the source code.
Any and all help will be appreciated!
I'm making an educated guess from memory, but I think you need to
implement your own dragMoveEvent() method and accept the event there
as well. This is because the item view widgets typically like to handle
all aspects of drag and drop, and QListView (from which QListWidget is
derived) ignores drag move events by default.

So, if you want to change the behaviour in a fundamental way, you need
to handle many aspects of the process; in this case, you need to
provide your own dragEnterEvent(), dragMoveEvent() and dropEvent()
methods. These can be simple, as shown in the following example:

class ListWidget(QListWidget):
def __init__(self, parent = None):
QListWidget.__init__(self, parent)
self.setAcceptDrops(True)

def dragEnterEvent(self, event):
event.acceptProposedAction()

def dragMoveEvent(self, event):
event.acceptProposedAction()

def dropEvent(self, event):
print "Drop"
event.accept()

listWidget = ListWidget()
listWidget.show()

I tested this code with Python 2.4.2, Qt 4.1.4 and PyQt 4.1, and I
think you should be able to do the same in your code.

Two other points:

1. If you are prepared to try Qt's model/view classes, you can exert
more control over the way drag and drop is handled. They're not
quite as straightforward as the item-based classes that you're
already using, but you benefit from greater reusability of your
components and better control over the appearance and behaviour
of the view widgets.

This guide is for C++, but it translates fairly well into Python:

http://doc.trolltech.com/4.1/model-v...ogramming.html

2. You seem to prefer adding methods to specific instances rather than
subclassing and reimplementing methods in the base class. I hope
you don't mind me asking, but is that because it takes less code,
because you find it conceptually easier, or is there another reason
that I'm missing?

David

Aug 25 '06 #2

David Boddie wrote:
>
I tested this code with Python 2.4.2, Qt 4.1.4 and PyQt 4.1, and I
think you should be able to do the same in your code.
First of all, thanks a lot for the very precise and quick help! Ot
works now.
1. If you are prepared to try Qt's model/view classes, you can exert
more control over the way drag and drop is handled. They're not
quite as straightforward as the item-based classes that you're
already using, but you benefit from greater reusability of your
components and better control over the appearance and behaviour
of the view widgets.
I will certainly give it more thought and probably reimplement this
program with model/view classes. Thanks for the heads-up.
2. You seem to prefer adding methods to specific instances rather than
subclassing and reimplementing methods in the base class. I hope
you don't mind me asking, but is that because it takes less code,
because you find it conceptually easier, or is there another reason
that I'm missing?
I am pretty new to Python and more so with PyQt4 and am still to learn
a lot of things. I realized the issue with adding methods to specific
instances a bit after I emailed to the group. I am going to subclass
the widget classes in the program.

Again, thanks a lot.

Cheers!

Harshad.

Aug 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lauren Quantrell | last post by:
I'm trying to drop a file from Windows Explorer (or desktop, etc.) onto a field in Access2K and capture the full file path. I found an posting below that says this is possible but I cannot...
2
by: SamSpade | last post by:
There seems to be two ways to put things on the clipboard ( I don't mean different formats): SetClipboardData and OleSetClipboard If I want to get data off the clipboard do I care how it was put...
3
by: Ajay Krishnan Thampi | last post by:
I have a slight problem implementing 'drag and drop' from a datagrid to a tree-view. I have pasted my code below. Someone please advice me on what to do...pretty blur right now. ==code== ...
6
by: jojobar | last post by:
Hello, I look at the asp.net 2.0 web parts tutorial on the asp.net web site. I tried to run it under firefox browser but it did not run. If I want to use this feature in a commercial product...
3
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
1
by: Darren | last post by:
I'm trying to create a file using drag and drop. I want to be able to select a listview item drag it to the shell and create a file. Each icon in the listview represents a blob in a database. When...
0
by: RHSFSS | last post by:
Hi, I have a Drag and Drop registration problem (See http://www.thescripts.com/forum/thread434707.html for similar problem post), can anyone out thereadvise on the best solution? I have a .NET 2.0 ...
2
by: jiang.haiyun | last post by:
Hi, I am having some serious problems with PyQT4, when i run pyqt script, I always get 'Segmentation fault'. the script is simple: ====================== %less qttest.py from PyQt4 import...
5
by: Romulo NF | last post by:
Greetings, Im back here to show the new version of the drag & drop table columns (original script ). Ive found some issues with the old script, specially when trying to use 2 tables with...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.