拖拽
在GUI裏,拖放是指用戶點擊一個虛擬的對象,拖動,然後放置到另外一個對象上面的動作。一般情況下,需要調用很多動作和方法,創建很多變量。
拖放能讓用戶很直觀的操作很複雜的邏輯。
一般情況下,我們可以拖放兩種東西:數據和圖形界面。把一個圖像從一個應用拖放到另外一個應用上的實質是操作二進制數據。把一個表格從Firefox上拖放到另外一個位置 的實質是操作一個圖形組。
簡單的拖放
本例使用了QLineEdit和QPushButton。把一個文本從編輯框裏拖到按鈕上,更新按鈕上的標籤(文字)。
import sys
from PyQt5.QtWidgets import (QPushButton, QWidget, QLineEdit, QApplication)
class Button(QPushButton):
def __init__(self, title, parent):
print("__init__======title==", title)
super().__init__(title, parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
print("dragEnterEvent========")
if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()
def dropEvent(self, e):
print("dropEvent========")
self.setText(e.mimeData().text())
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
edit = QLineEdit("", self)
edit.setMaximumSize(50, 30)
edit.setDragEnabled(True)
edit.move(30, 65)
button = Button("Button", self)
button.setMaximumSize(100, 30)
button.move(190, 65)
self.setWindowTitle("Simple drag & drop")
self.setGeometry(300, 300, 300, 150)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
爲了完成預定目標,我們要重構一些方法。首先用QPushButton上構造一個按鈕實例。
self.setAcceptDrops(True)
激活組件的拖拽事件。
def dragEnterEvent(self, e): if e.mimeData().hasFormat('text/plain'): e.accept() else: e.ignore()
首先,我們重構了dragEnterEvent()方法。設定好接受拖拽的數據類型(plain text)。
def dropEvent(self, e): self.setText(e.mimeData().text())
然後重構dropEvent()方法,更改按鈕接受鼠標的釋放事件的默認行爲。
edit = QLineEdit('', self) edit.setDragEnabled(True)
QLineEdit默認支持拖拽操作,所以我們只要調用setDragEnabled()方法使用就行了。
程序展示:
拖放按鈕組件
這個例子展示怎麼拖放一個button組件。
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())
dropAction = 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_()
上面的例子中,窗口上有一個QPushButton組件。左鍵點擊按鈕,控制檯就會輸出press。右鍵可以點擊然後拖動按鈕。
class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent)
從QPushButton繼承一個Button類,然後重構QPushButton的兩個方法:mouseMoveEvent()和mousePressEvent().mouseMoveEvent()是拖拽開始的事件。
if e.buttons() != Qt.RightButton: return
我們只劫持按鈕的右鍵事件,左鍵的操作還是默認行爲。
mimeData = QMimeData() drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft())
創建一個QDrag對象,用來傳輸MIME-based數據。
dropAction = drag.exec_(Qt.MoveAction)
拖放事件開始時,用到的處理函數式start().
def mousePressEvent(self, e): QPushButton.mousePressEvent(self, e) if e.button() == Qt.LeftButton: print('press')
左鍵點擊按鈕,會在控制檯輸出“press”。注意,我們在父級上也調用了mousePressEvent()方法,不然的話,我們是看不到按鈕按下的效果的。
position = e.pos() self.button.move(position)
在dropEvent()方法裏,我們定義了按鈕按下後和釋放後的行爲,獲得鼠標移動的位置,然後把按鈕放到這個地方。
e.setDropAction(Qt.MoveAction) e.accept()
指定放下的動作類型爲moveAction。
程序展示:
參考:
https://maicss.gitbooks.io/pyqt5/
http://zetcode.com/gui/pyqt5/
http://code.py40.com/pyqt5/