- PyQt - QPixmap Class
- PyQt - QClipboard
- PyQt - BrushStyle Constants
- PyQt - Drawing API
- PyQt - Database Handling
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QMessageBox
- PyQt - QDialog Class
- PyQt - Basic Widgets
- PyQt - Layout Management
- PyQt - Signals and Slots
- PyQt - Using Qt Designer
- PyQt - Major Classes
- PyQt - Hello World
- PyQt - Introduction
- PyQt - Home
PyQt Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
PyQt - Drag & Drop
The provision of drag and drop is very intuitive for the user. It is found in many desktop apppcations where the user can copy or move objects from one window to another.
MIME based drag and drop data transfer is based on QDrag class. QMimeData objects associate the data with their corresponding MIME type. It is stored on cppboard and then used in the drag and drop process.
The following QMimeData class functions allow the MIME type to be detected and used conveniently.
Tester | Getter | Setter | MIME Types |
---|---|---|---|
hasText() | text() | setText() | text/plain |
hasHtml() | html() | setHtml() | text/html |
hasUrls() | urls() | setUrls() | text/uri-pst |
hasImage() | imageData() | setImageData() | image/ * |
hasColor() | colorData() | setColorData() | apppcation/x-color |
Many QWidget objects support the drag and drop activity. Those that allow their data to be dragged have setDragEnabled() which must be set to true. On the other hand, the widgets should respond to the drag and drop events in order to store the data dragged into them.
DragEnterEvent provides an event which is sent to the target widget as dragging action enters it.
DragMoveEvent is used when the drag and drop action is in progress.
DragLeaveEvent is generated as the drag and drop action leaves the widget.
DropEvent, on the other hand, occurs when the drop is completed. The event’s proposed action can be accepted or rejected conditionally.
Example
In the following code, the DragEnterEvent verifies whether the MIME data of the event contains text. If yes, the event’s proposed action is accepted and the text is added as a new item in the ComboBox.
import sys from PyQt4.QtGui import * from PyQt4.QtCore import * class combo(QComboBox): def __init__(self, title, parent): super(combo, self).__init__( parent) self.setAcceptDrops(True) def dragEnterEvent(self, e): print e if e.mimeData().hasText(): e.accept() else: e.ignore() def dropEvent(self, e): self.addItem(e.mimeData().text()) class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): lo = QFormLayout() lo.addRow(QLabel("Type some text in textbox and drag it into combo box")) edit = QLineEdit() edit.setDragEnabled(True) com = combo("Button", self) lo.addRow(edit,com) self.setLayout(lo) self.setWindowTitle( Simple drag & drop ) def main(): app = QApppcation(sys.argv) ex = Example() ex.show() app.exec_() if __name__ == __main__ : main()
The above code produces the following output −
Advertisements