Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How PyQt5 uses mimeData to implement drag events

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how PyQt5 uses mimeData to achieve drag and drop events, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Realization idea

1. A brief introduction to QMimeData

2. Use case 1 of QMimeData: implement text drag and drop of input box in QT

3. Use case 2 of QMimeData: implementing button dragging in QT

The implementation of the two use cases is as follows:

Use case 1:

Use case 2:

1. Brief introduction of QMimeData1 and QDrag

The first step is to create a QDrag, which can be created in mousePressEvent, mouseMoveEvent, or dragMoveEvent.

QDrag before exec, be sure to set QMimeData, otherwise the drag operation will not start.

QMimeData is very useful in dragging and dropping. It can be used to save information that comes with a drag-and-drop operation, such as a string, file, or picture. It can also be used to verify the format of the saved information and to determine whether it is acceptable or not.

Also note that under windows, QDrag::exec () is a synchronous operation, and the following code does not continue until exec () returns.

II. Events related to drag

First, when you need a control to receive drag and drop, you first call the control's method: setAcceptDrops (True).

There are three drag-related events in qt, dragEnterEvent, dragMoveEvent, and dragLeaveEvent. These three events trigger conditions similar to mouse move in, mouse move, mouse move out. When the mouse drags into the control and triggers the dragEnterEvent, the drag movement within the control triggers the dragMoveEvent, and the mouse drags away from the control triggers the dragLeaveEvent.

III. DropEvent

When the drag is in the accept state, and then release the mouse, a dropEvent is generated. We can handle the Mime information attached to this drag and drop in this event.

Drag and drop key logic diagrams

2. Use case 1 of QMimeData implements text drag #-*-coding: utf-8-*-import sysfrom PyQt5.QtCore import Qt, QMimeDatafrom PyQt5.QtGui import QDragfrom PyQt5.QtWidgets import QWidget, QLineEdit, QApplication, QSplitter, QHBoxLayoutclass MyLineEdit (QLineEdit): def _ init__ (self, parent): super (). _ init__ (parent) self.setAcceptDrops (True) def dragMoveEvent (self) in QT Event): drag = QDrag (self) mime = QMimeData () drag.setMimeData (mime) drag.exec (Qt.CopyAction) def dragEnterEvent (self, event): if event.mimeData () .hasText (): event.accept () else: event.ignore () def dropEvent (self Event): self.setText (event.mimeData (). Text () event.source (). SetText (") class SimpleDrag (QWidget): def _ init__ (self): super (). _ init__ () self.initUI () def initUI (self): hlayout = QHBoxLayout (self) edit1 = MyLineEdit (self) edit1.setDragEnabled (True) Edit2 = MyLineEdit (self) edit2.setDragEnabled (True) splitter = QSplitter (Qt.Horizontal) splitter.addWidget (edit1) splitter.addWidget (edit2) hlayout.addWidget (splitter) self.setLayout (hlayout) self.setWindowTitle ('easy drag event') if _ name__ ='_ main__': app = QApplication (sys.argv) ex = SimpleDrag () Ex.show () app.exec_ ()

Key resolution:

In a custom control:

1. We have created an input box for QLineEdit inherited from Qt

2. Create the QDrag in dragMoveEvent, set the mimeData of drag, and then call the exec method on QDrag

3. Received the event in dragEnterEvent, that is, the event.accept () of the corresponding code.

4. The event is handled in dropEvent

In the main window:

1. Set the window to receive the drag event setDragEnabled (True)

This perfectly corresponds to the use of the QMimeData above.

3. Use case 2 of QMimeData: drag the QT implementation button #-*-coding: utf-8-*-import sysfrom PyQt5.QtWidgets import QPushButton, QWidget, QApplicationfrom PyQt5.QtCore import Qt, QMimeDatafrom PyQt5.QtGui import QDragclass Button (QPushButton): def _ init__ (self, title, parent): super (). _ init__ (title, parent) def mouseMoveEvent (self) E): if e.buttons ()! = Qt.LeftButton: return mimeData = QMimeData () drag = QDrag (self) drag.setMimeData (mimeData) drag.setHotSpot (e.pos ()-self.rect (). TopLeft () drag.exec_ (Qt.MoveAction) class Example (QWidget): def _ init__ (self): super (). _ _ init__ () self.initUI () def initUI (self): self.setAcceptDrops (True) self.button = Button ('Button') Self) self.button.move (100,65) self.setWindowTitle ('Click or Move') self.setGeometry (300,300,280150) def dragEnterEvent (self, e): e.accept () def dropEvent (self E): position = e.pos () self.button.move (position) e.setDropAction (Qt.MoveAction) e.accept () if _ _ name__ ='_ _ main__': app = QApplication (sys.argv) ex = Example () ex.show () app.exec_ ()

Key resolution:

In a custom control:

1. We have created a button that inherits QPushButton from Qt

2. Create the QDrag in mouseMoveEvent, set the mimeData of drag, and then call the exec method on QDrag

In the main window:

1. Set the window to receive the drag event setDragEnabled (True)

2. Handle the event in dropEvent and change the position of the button.

1. Received the event in dragEnterEvent, that is, the event.accept () of the corresponding code.

The second example is a little different from the first, because in the first example, the put event is given to the input box MyLineEdit

In the second example, the control that receives the play event at this time is the main window Example (QWidget)

These are all the contents of the article "how PyQt5 uses mimeData to implement drag and drop events". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report