日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

pyqt5教程12:拖放功能

發(fā)布時(shí)間:2025/3/21 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pyqt5教程12:拖放功能 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

????????在 PyQt5 教程的這一部分中,我們將討論拖放操作。

????????在計(jì)算機(jī)圖形用戶界面中,拖放是單擊虛擬對(duì)象并將其拖動(dòng)到不同位置或另一個(gè)虛擬對(duì)象上的動(dòng)作(或支持動(dòng)作)。一般來說,它可以用來調(diào)用多種動(dòng)作,或者在兩個(gè)抽象對(duì)象之間創(chuàng)建各種類型的關(guān)聯(lián)。

????????拖放是圖形用戶界面的一部分。拖放操作使用戶能夠直觀地做復(fù)雜的事情。

????????通常,我們可以拖放兩件事:數(shù)據(jù)或一些圖形對(duì)象。如果我們將圖像從一個(gè)應(yīng)用程序拖到另一個(gè)應(yīng)用程序,我們拖放二進(jìn)制數(shù)據(jù)。如果我們?cè)?Firefox 中拖動(dòng)一個(gè)選項(xiàng)卡并將其移動(dòng)到另一個(gè)位置,我們就會(huì)拖放一個(gè)圖形組件。

1 QDrag

????????QDrag 支持基于 MIME 的拖放數(shù)據(jù)傳輸。它處理拖放操作的大部分細(xì)節(jié)。傳輸?shù)臄?shù)據(jù)包含在 QMimeData 對(duì)象中。

2 簡(jiǎn)單拖放PyQt5

????????在第一個(gè)示例中,我們有一個(gè) QLineEdit 和一個(gè) QPushButton。我們將純文本從行編輯小部件拖放到按鈕小部件上。按鈕的標(biāo)簽會(huì)改變。

simple_dragdrop.py

#!/usr/bin/python""" ZetCode PyQt5 tutorialThis is a simple drag and drop example.Author: Jan Bodnar Website: zetcode.com """import sysfrom 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 and drop')self.setGeometry(300, 300, 300, 150)def main():app = QApplication(sys.argv)ex = Example()ex.show()app.exec_()if __name__ == '__main__':main()

該示例展示了一個(gè)簡(jiǎn)單的拖放操作。

class Button(QPushButton):def __init__(self, title, parent):super().__init__(title, parent)...

????????為了將文本放在QPushButton 小部件,我們必須重新實(shí)現(xiàn)一些方法。因此,我們創(chuàng)建了自己的 Button 類,該類繼承自 QPushButton 類。

self.setAcceptDrops(True)

我們使用 setAcceptDrops 為小部件啟用放置事件。

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

????????首先,我們重新實(shí)現(xiàn)了 dragEnterEvent 方法。我們告知我們接受的數(shù)據(jù)類型。在我們的例子中,它是純文本。

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

????????通過重新實(shí)現(xiàn) dropEvent 方法,我們定義了 drop 事件發(fā)生了什么。在這里,我們更改按鈕小部件的文本。

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

????????QLineEdit 小部件內(nèi)置了對(duì)拖動(dòng)操作的支持。我們需要做的就是調(diào)用 setDragEnabled 方法來激活它。

Figure: Simple drag and drop

3 窗口之間拖放

以下示例演示如何拖放按鈕小部件。

drag_button.py

#!/usr/bin/python""" ZetCode PyQt5 tutorialIn 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 """import sysfrom PyQt5.QtCore import Qt, QMimeData from PyQt5.QtGui import QDrag from PyQt5.QtWidgets import QPushButton, QWidget, QApplicationclass Button(QPushButton):def __init__(self, title, parent):super().__init__(title, parent)def mouseMoveEvent(self, e):if e.buttons() != Qt.RightButton:returnmimeData = QMimeData()drag = QDrag(self)drag.setMimeData(mimeData)drag.setHotSpot(e.pos() - self.rect().topLeft())dropAction = drag.exec_(Qt.MoveAction)def mousePressEvent(self, e):super().mousePressEvent(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, 550, 450)def dragEnterEvent(self, e):e.accept()def dropEvent(self, e):position = e.pos()self.button.move(position)e.setDropAction(Qt.MoveAction)e.accept()def main():app = QApplication(sys.argv)ex = Example()ex.show()app.exec_()if __name__ == '__main__':main()

在我們的代碼示例中,我們?cè)诖翱谏嫌幸粋€(gè) QPushButton。如果我們用鼠標(biāo)左鍵單擊按鈕,“按下”消息將打印到控制臺(tái)。通過右鍵單擊并移動(dòng)按鈕,我們對(duì)按鈕小部件執(zhí)行拖放操作。

class Button(QPushButton):def __init__(self, title, parent):super().__init__(title, parent)

我們創(chuàng)建一個(gè)派生自 QPushButton 的 Button 類。我們還重新實(shí)現(xiàn)了 QPushButton 的兩個(gè)方法:mouseMoveEvent 和 mousePressEvent。 mouseMoveEvent 方法是拖放操作開始的地方。

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

在這里,我們決定只能使用鼠標(biāo)右鍵執(zhí)行拖放操作。鼠標(biāo)左鍵保留用于單擊該按鈕。

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

已創(chuàng)建 QDrag 對(duì)象。該類提供對(duì)基于 MIME 的拖放數(shù)據(jù)傳輸?shù)闹С帧?/p> dropAction = drag.exec_(Qt.MoveAction)

拖動(dòng)對(duì)象的 exec_ 方法啟動(dòng)拖放操作。

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

如果我們用鼠標(biāo)左鍵單擊按鈕,我們會(huì)將“按下”打印到控制臺(tái)。請(qǐng)注意,我們也在父級(jí)上調(diào)用了 mousePressEvent 方法。否則,我們將看不到按鈕被按下。

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

在 dropEvent 方法中,我們指定釋放鼠標(biāo)按鈕并完成放置操作后會(huì)發(fā)生什么。在我們的例子中,我們找出當(dāng)前鼠標(biāo)指針的位置并相應(yīng)地移動(dòng)按鈕。

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

我們使用 setDropAction 指定放置動(dòng)作的類型。在我們的例子中,它是一個(gè)移動(dòng)動(dòng)作。

總結(jié)

以上是生活随笔為你收集整理的pyqt5教程12:拖放功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。