日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pyqt5教程12:拖放功能

發布時間:2025/3/21 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pyqt5教程12:拖放功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

????????在計算機圖形用戶界面中,拖放是單擊虛擬對象并將其拖動到不同位置或另一個虛擬對象上的動作(或支持動作)。一般來說,它可以用來調用多種動作,或者在兩個抽象對象之間創建各種類型的關聯。

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

????????通常,我們可以拖放兩件事:數據或一些圖形對象。如果我們將圖像從一個應用程序拖到另一個應用程序,我們拖放二進制數據。如果我們在 Firefox 中拖動一個選項卡并將其移動到另一個位置,我們就會拖放一個圖形組件。

1 QDrag

????????QDrag 支持基于 MIME 的拖放數據傳輸。它處理拖放操作的大部分細節。傳輸的數據包含在 QMimeData 對象中。

2 簡單拖放PyQt5

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

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()

該示例展示了一個簡單的拖放操作。

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

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

self.setAcceptDrops(True)

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

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

????????首先,我們重新實現了 dragEnterEvent 方法。我們告知我們接受的數據類型。在我們的例子中,它是純文本。

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

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

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

????????QLineEdit 小部件內置了對拖動操作的支持。我們需要做的就是調用 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()

在我們的代碼示例中,我們在窗口上有一個 QPushButton。如果我們用鼠標左鍵單擊按鈕,“按下”消息將打印到控制臺。通過右鍵單擊并移動按鈕,我們對按鈕小部件執行拖放操作。

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 的拖放數據傳輸的支持。

dropAction = drag.exec_(Qt.MoveAction)

拖動對象的 exec_ 方法啟動拖放操作。

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

如果我們用鼠標左鍵單擊按鈕,我們會將“按下”打印到控制臺。請注意,我們也在父級上調用了 mousePressEvent 方法。否則,我們將看不到按鈕被按下。

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

在 dropEvent 方法中,我們指定釋放鼠標按鈕并完成放置操作后會發生什么。在我們的例子中,我們找出當前鼠標指針的位置并相應地移動按鈕。

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

我們使用 setDropAction 指定放置動作的類型。在我們的例子中,它是一個移動動作。

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。