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



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


本節(jié)教程講解了拖放操作。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Pytorch+PyG實(shí)現(xiàn)GCN過(guò)程示例
這篇文章主要為大家介紹了Pytorch+PyG實(shí)現(xiàn)GCN過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Python常見(jiàn)錯(cuò)誤:IndexError:?list?index?out?of?range解決
最近在寫(xiě)一個(gè)爬蟲(chóng)程序,但是卻出現(xiàn)了錯(cuò)誤提示IndexError:?list?index?out?of?range,所以下面這篇文章主要給大家介紹了關(guān)于Python常見(jiàn)錯(cuò)誤:IndexError:?list?index?out?of?range的解決方法,需要的朋友可以參考下2023-01-01
python-pymongo常用查詢(xún)方法含聚合問(wèn)題
這篇文章主要介紹了python-pymongo常用查詢(xún)方法含聚合問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
基于Python開(kāi)發(fā)電腦定時(shí)關(guān)機(jī)工具
這篇文章主要為大家詳細(xì)介紹了如何基于Python開(kāi)發(fā)一個(gè)電腦定時(shí)關(guān)機(jī)工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-01-01
全面了解Python環(huán)境配置及項(xiàng)目建立
下面小編就為大家?guī)?lái)一篇全面了解Python環(huán)境配置及項(xiàng)目建立。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06

