|
在 PyQt4 中 drag 和 drop
本部分,我們將討論 drag 和 drop 操作。
在計算機中的圖形界面中, drag-and-drop 是例如點擊到一個虛擬對象并把它拖到另外的位置上的行為。一般來說,這可以用于很多行為,或創(chuàng)建兩個對象間的關聯(lián)。(Wikipedia)
drag 和 drop 的功能是 GUI 最有用的功能之一。它可以是用戶處理復雜的工作。
一般來說,我們可以 drag 和 drop 兩種東西,數(shù)據(jù)或圖形對象。如果我們吧一幅圖像從一個應用拖到另一個應用,我們處理的是二進制數(shù)據(jù)。如果我們在 Firefox 中拖動了一個標簽,我們拖的則是一個圖形組件。
簡單的 Drag 和 Drop
第一個例子,我們將有一個 QtGui.QLineEdit 和 QtGui.QPushButton 。我們將從行編輯區(qū)拖動文本到按鈕上。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
This is a simple drag and
drop example.
author: Jan Bodnar
website: zetcode.com
last edited: December 2010
"""
import sys
from PyQt4 import QtGui
class Button(QtGui.QPushButton):
def __init__(self, title, parent):
super(Button, self).__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(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
edit = QtGui.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)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
if __name__ == '__main__':
main()
簡單的拖拽操作。
class Button(QtGui.QPushButton):
def __init__(self, title, parent):
super(Button, self).__init__(title, parent)
為了可以把文字拖到 QtGui.QPushButton 組件上,我們必需要重新實現(xiàn)一些方法。所以我們創(chuàng)建了我們自己的按鈕類。它從 QtGui.QPushButton 派生。
self.setAcceptDrops(True)
我們開啟允許接受拖入的事件。
def dragEnterEvent(self, e):
if e.mimeDate().hasFormat('text/plain'):
e.accept()
else:
e.ignore()
首先,我們重新實現(xiàn)了 drageEnterEvent() 方法。我們將接受特定的數(shù)據(jù)類型,此處是純文本。
def dropEvent(self, e):
self.setText(e.mimeDate().text())
通過重新實現(xiàn) dropEvent() 方法,我們定義了放下后處理的事件。我們在這里是改變了按鈕中的顯示文本。
edit = QtGui.QLineEdit('', self)
edit.setDragEnabled(True)
QtGui.QLineEdit 組件有內(nèi)置的拖拽操作。我們只需要調(diào)用 setDragEnabled() 激活它就可以了。
拖拽一個按鈕組件
下面的例子,我們將介紹如何拖拽一個按鈕對象。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this program, we can press
on a button with a left mouse
click or drag and drop the button
with the right mouse click.
author: Jan Bodnar
website: zetcode.com
last edited: December 2010
"""
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Button(QtGui.QPushButton):
def __init__(self, title, parent):
super(Button, self).__init__(title, parent)
def mouseMoveEvent(self, e):
if e.buttons() != QtCore.Qt.RightButton:
return
mimeData = QtCore.QMimeData()
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft())
dropAction = drag.start(QtCore.Qt.MoveAction)
def mousePressEvent(self, e):
QtGui.QPushButton.mousePressEvent(self, e)
if e.button() == QtCore.Qt.LeftButton:
print 'press'
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__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(QtCore.Qt.MoveAction)
e.accept()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
if __name__ == '__main__':
main()
在這個例子中,我們有一個 QtGui.QPushButton 在窗口上。如果我們點擊按鈕,將在控制臺上輸出 ‘press’ 。而如果右擊按鈕并且移動,我們就可以拖拽這個按鈕組件。
class Button(QtGui.QPushButton):
def __init__(self, title, parent):
super(Button, self).__init__(title, parent)
我們創(chuàng)建了一個派生自 QtGui.QPushButton 的按鈕類。我們還重新實現(xiàn)了 QtGui.QPushButton 中的兩個方法, mouseMoveEvent() 和 mousePressEvent() 。其中, mouseMoveEvent() 方法是開始拖拽處發(fā)生的地方。
if event.buttons() != QtCore.Qt.RightButton:
return
我們決定只用鼠標右擊進行拖拽。左擊用于點擊按鈕。
mimeData = QtCore.QMimeData()
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(event.pos() - self.rect().topLeft())
我們創(chuàng)建了一個 QDrag 對象。
dropAction = drag.start(QtCore.Qt.MoveAction)
start() 方法開始拖拽操作。
def mousePressEvent(self, e):
QtGui.QPushButton.mousePressEvent(self, e)
if e.button() == QtCore.Qt.LeftButton:
print 'press'
如果點擊了鼠標左鍵,我們在控制臺上打印 ‘press’ 。注意,我們還調(diào)用了父類的 mousePressEvent() 方法。否則,我們將不會看到按鈕被按下。
position = e.pos()
self.button.move(position)
在 dropEvent() 方法中,定義了當我們松開鼠標按鈕停止拖拽的行為。我們找到鼠標當前的位置,并把按鈕移到合適的位置。
e.setDropAction(QtCore.Qt.MoveAction)
e.accept()
我們指定了拖拽的類型。在此處是移動的行為。
本部分我們討論了拖拽。
|