PyQt5實現(xiàn)拖放功能
在這節(jié)教程中,我們將探討PyQt5中的拖放操作。
在計算機圖形用戶界面(GUI)中,拖放是在某個虛擬對象上點擊并拖動到另一個位置或虛擬對象上的操作。它通常用于調(diào)用多個動作,或為兩個抽象對象創(chuàng)建某些聯(lián)系。
拖放是圖形用戶界面的一部分。拖放可以使用戶直觀地完成某些復(fù)雜的操作。
通常我們可以對兩種事物進(jìn)行拖放操作:數(shù)據(jù)或某些圖形對象。如果我們將某個應(yīng)用中的圖片拖放到另一個應(yīng)用,我們拖放的是二進(jìn)制數(shù)據(jù)。如果將Firefox的某個標(biāo)簽頁拖放到其他地方,我們拖放的是一個圖形組件。
簡單的拖放
在第一個示例中我們要創(chuàng)建一個QLineEdit和一個QPushButton,并通過將LineEdit中的文本拖放到按鈕上來改變按鈕的標(biāo)簽。
import sys
from PyQt5.QtWidgets import (QPushButton, QWidget, QLineEdit, QApplication)
class 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.setWindowTitle("Simple drag & drop")
self.setGeometry(300, 300, 300, 150)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
這個示例演示了一個簡單的拖放操作。
class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) self.setAcceptDrops(True)
我們需要重新實現(xiàn)某些方法才能使QPushButton接受拖放操作。因此我們創(chuàng)建了繼承自QPushButton的Button類。
self.setAcceptDrops(True)
使該控件接受drop(放下)事件。
def dragEnterEvent(self, e):
if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()
首先我們重新實現(xiàn)了dragEnterEvent()方法,并設(shè)置可接受的數(shù)據(jù)類型(在這里是普通文本)。
def dropEvent(self, e): self.setText(e.mimeData().text())
通過重新實現(xiàn)dropEvent()方法,我們定義了在drop事件發(fā)生時的行為。這里我們改變了按鈕的文字。
edit = QLineEdit('', self)
edit.setDragEnabled(True)
QLineEdit內(nèi)置了對drag(拖動)操作的支持。我們只需要調(diào)用setDragEnabled()方法就可以了。



拖放一個按鈕
在下面的示例中我們將演示如何對一個按鈕控件進(jìn)行拖放。
import sys
from PyQt5.QtWidgets import QPushButton, QWidget, QApplication
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
class 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())
dropAcion = 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.setWindowTitle("Click or Move")
self.setGeometry(300, 300, 280, 150)
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_()
我們在窗體中創(chuàng)建了一個QPushButton。如果用鼠標(biāo)左鍵點擊這個按鈕會在控制臺中輸出'press'消息。我們在這個按鈕上實現(xiàn)了拖放操作,可以通過鼠標(biāo)右擊進(jìn)行拖動。
class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent)
我們從QPushButton派生了一個Button類,并重新實現(xiàn)了mouseMoveEvent()與mousePressEvent()方法。mouseMoveEvent()方法是拖放操作產(chǎn)生的地方。
if e.buttons() != Qt.RightButton: return
在這里我們設(shè)置只在鼠標(biāo)右擊時才執(zhí)行拖放操作。鼠標(biāo)左擊用于按鈕的點擊事件。
mimeData = QMimeData() drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft())
QDrag提供了對基于MIME的拖放的數(shù)據(jù)傳輸?shù)闹С帧?/p>
dropAction = drag.exec_(Qt.MoveAction)
Drag對象的exec_()方法用于啟動拖放操作。
def mousePressEvent(self, e):
QPushButton.mousePressEvent(self, e)
if e.button() == Qt.LeftButton:
print('press')
鼠標(biāo)左擊按鈕時我們會在控制臺打印‘press'。注意我們也調(diào)用了父按鈕的mousePressEvent()方法。否則會看不到按鈕的按下效果。
position = e.pos() self.button.move(position)
在dropEvent()方法中,我們要為松開鼠標(biāo)后的操作進(jìn)行編碼,并完成drop操作。即找出鼠標(biāo)指針的當(dāng)前位置,并將按鈕移動過去。
e.setDropAction(Qt.MoveAction) e.accept()
我們定義了drop動作的類型。這里是move動作。


本節(jié)教程講解了拖放操作。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python常見錯誤:IndexError:?list?index?out?of?range解決
最近在寫一個爬蟲程序,但是卻出現(xiàn)了錯誤提示IndexError:?list?index?out?of?range,所以下面這篇文章主要給大家介紹了關(guān)于Python常見錯誤:IndexError:?list?index?out?of?range的解決方法,需要的朋友可以參考下2023-01-01

