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

Example Analysis of drag and drop events in PyQt5

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the example analysis of drag and drop events in PyQt5, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

In the computer graphical user interface, a drag-and-drop event is the act of clicking on a virtual object and dragging it to another location or to another virtual object. In general, it can be used to invoke multiple actions or to create various types of associations between two abstract objects.

Drag and drop events are part of the graphical user interface. Drag-and-drop operations enable users to intuitively manipulate complex things.

In general, we can drag and drop two types: data or some graphical objects. If we drag images from one application to another, we drag and drop binary data. If we drag and drop the Firefox tag and move it to another place, we drag and drop the graphics component.

Simple drag and drop event

In this example, we have a QLineEdit control and a QPushButton control. We select the entered text from the single-line text editing control, drag it onto the button control and release the mouse, and the label of the button will change.

#! / usr/bin/python3#-*-coding: utf-8-*-"" PyQt5 tutorial this is a simple drag-and-drop example. Author: my world you have been to the blog: http://blog.csdn.net/weiaitaowang Last Editor: "import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButtonclass Button (QPushButton): def _ _ init__ (self, title, parent): super (). _ init__ (title, parent) self.setAcceptDrops (True) def dragEnterEvent (self) E): if e.mimeData (). HasFormat ('text/plain'): e.accept () else: e.ignore () def dropEvent (self, e): self.setText (e.mimeData (). Text ()) class Example (QWidget): def _ init__ (self): super (). _ init__ () self.initUI () def initUI (self): edit = QLineEdit (', self) edit.setDragEnabled (True) edit.move (30,65) button = Button ('button') Self) button.move (190,65) self.setGeometry (300,300,150) self.setWindowTitle ('simple drag and drop') if _ _ name__ = ='_ main__': app = QApplication (sys.argv) ex = Example () ex.show () sys.exit (app.exec_ ())

This example introduces a simple drag-and-drop operation.

Class Button (QPushButton): def _ init__ (self, title, parent): super (). _ init__ (title, parent) self.setAcceptDrops (True)

In order to display placed text in the QPushButton control, we must override some of the methods of the QPushButton control. Therefore, we create our own button class that will inherit from the QPushButton class.

Self.setAcceptDrops (True)

Enables drag-and-drop events for the control.

Def dragEnterEvent (self, e): if e.mimeData () .hasFormat ('text/plain'): e.accept () else: e.ignore ()

First, the dragEnterEvent () method is overridden. Tell us the type of data we accept (text/plain). Typically, it is plain text.

Def dropEvent (self, e): self.setText (e.mimeData () .text ())

Next, you rewrite the dropEvent () method, which defines what the drop event will do. Here we change the text of the button control.

Edit = QLineEdit ('', self) edit.setDragEnabled (True)

To enable drag on the QLineEdit control, all you need to do is call the setDragEnabled () method to activate it.

After the program is executed

Drag and drop button control

In the following example, we will demonstrate how to drag and drop a button control.

#! / usr/bin/python3#-*-coding: utf-8-*-PyQt5 tutorial in this program, we can click or drag a button with the left mouse button and click the delete button with the right mouse button. Author: my world you have been to the blog: http://blog.csdn.net/weiaitaowang Last Editor: "import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QPushButtonfrom 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.RightButton: return mimeData = QMimeData () drag = QDrag (self) drag.setMimeData (mimeData) drag.setHotSpot (e.pos ()-self.rect (). TopLeft () drag.exec_ (Qt.MoveAction) def mousePressEvent (self, e): QPushButton.mousePressEvent (self E) if e.button () = Qt.LeftButton: print ('press') 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.setGeometry (300,300,280150) self.setWindowTitle ('button drag and drop') 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 () sys.exit (app.exec_ ())

In our code example, the window has a QPushButton button. If we press the button with the left mouse button, the 'press' message is printed to the console. If you hold down the button with the right mouse button and move the mouse, the program executes a drag-and-drop button control event.

Class Button (QPushButton): def _ init__ (self, title, parent): super (). _ init__ (title, parent)

Create a Button class that derives from QPushButton. We also rewrote two methods of QPushButton: mouseMoveEvent () and mousePressEvent (). The mouseMoveEvent () method is where the drag-and-drop operation begins.

If e.buttons ()! = Qt.RightButton: return

Here, we confirm that you can only use the right mouse button to perform drag-and-drop events. The left mouse button is reserved for the button click event.

MimeData = QMimeData () drag = QDrag (self) drag.setMimeData (mimeData) drag.setHotSpot (e.pos ()-self.rect (). TopLeft ())

Create a QDrag object. This class provides support for MIME-based drag-and-drop data transfer.

Drag.exec_ (Qt.MoveAction)

Drag the object's start () start method.

Def mousePressEvent (self, e): QPushButton.mousePressEvent (self, e) if e.button () = = Qt.LeftButton: print ('press')

If we click the button with the left mouse button, print 'press' to the console. Notice that we use the mousePressEvent () method to get mouse button information.

Position = e.pos () self.button.move (position)

The code in the dropEvent () method tells us to release the mouse button to complete the drag and drop operation. Find the current mouse pointer position and move the button to the appropriate location.

E.setDropAction (Qt.MoveAction) e.accept ()

We specify the type of placement action. In the current situation, it is a moving action.

After the program is executed

Thank you for reading this article carefully. I hope the article "sample Analysis of drag and drop events in PyQt5" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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