欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5每天必學(xué)之拖放事件

 更新時間:2020年08月27日 09:04:21   作者:我的世界你曾經(jīng)來過  
這篇文章主要為大家詳細介紹了PyQt5每天必學(xué)之拖放事件的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

在電腦圖形用戶界面,拖放事件就是點擊一個虛擬對象,并將其拖動到其他位置或到另一個虛擬物體的動作。在一般情況下,它可以被用于調(diào)用多種動作,或創(chuàng)建兩個抽象對象之間的關(guān)聯(lián)的各種類型。

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

通常情況下,我們可以拖放兩種類型:數(shù)據(jù)或某些圖形對象。如果我們從一個應(yīng)用程序拖動圖像到另一個,我們拖放的是二進制數(shù)據(jù)。如果我們拖放Firefox標簽并將其移動到另一個地方,我們拖放的是圖形組件。

簡單拖放事件

在這個例子中,我們有一個QLineEdit控件和一個QPushButton控件。我們從單行文本編輯控件中將輸入的文本選中后拖到按鈕控件上后松開鼠標,按鈕的標簽將發(fā)生變化。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

這是一個簡單的拖放例子。

作者:我的世界你曾經(jīng)來過
博客:http://blog.csdn.net/weiaitaowang
最后編輯:2016年8月5日
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton

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('按鈕', self)
 button.move(190, 65)

 self.setGeometry(300, 300, 300, 150)
 self.setWindowTitle('簡單拖放') 

if __name__ == '__main__':

 app = QApplication(sys.argv)
 ex = Example()
 ex.show()
 sys.exit(app.exec_())

這個例子介紹了一個簡單的拖放操作。

class Button(QPushButton):

 def __init__(self, title, parent):
 super().__init__(title, parent)
 self.setAcceptDrops(True)

為了在QPushButton控件中顯示放置的文字,我們必須將QPushButton控件的一些方法重寫。因此,我們創(chuàng)造我們自己的按鈕類將從QPushButton類繼承。

 self.setAcceptDrops(True)

為控件啟用拖放事件。

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

首先,重寫了dragEnterEvent()方法。告知我們接受的數(shù)據(jù)類型(text/plain)。通常情況下,它是純文本。

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

接下來重寫了dropEvent()方法,這里定義了drop事件將要做的事情。在這里我們改變按鈕控件的文本。

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

若要啟用QLineEdit控件的拖動操作,需要做的是調(diào)用setDragEnabled()方法來激活它。

程序執(zhí)行后

這里寫圖片描述

拖放按鈕控件

在下面的例子中,我們將演示如何拖放一個按鈕控件。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

在這個程序中,我們可以按上用鼠標左鍵點擊或拖動一個按鈕,用鼠標右鍵單擊刪除按鈕。

作者:我的世界你曾經(jīng)來過
博客:http://blog.csdn.net/weiaitaowang
最后編輯:2016年8月5日
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
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())
 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('按鈕', self)
 self.button.move(100, 65)

 self.setGeometry(300, 300, 280, 150)
 self.setWindowTitle('按鈕拖放')

 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()
 sys.exit(app.exec_())

在我們的代碼示例中,窗口有一個QPushButton 按鈕。如果我們用鼠標左鍵按下按鈕,'press' 消息打印到控制臺。如果用鼠標右鍵按住按鈕并移動鼠標,程序?qū)?zhí)行一個拖放按鈕控件事件。

class Button(QPushButton):

 def __init__(self, title, parent):
 super().__init__(title, parent)

創(chuàng)建一個Button 類從QPushButton派生。我們還重寫了QPushButton的兩種方法:mouseMoveEvent()和mousePressEvent()。該mouseMoveEvent()方法是其中拖放操作開始的地方。

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

在這里,我們確認執(zhí)行拖放事件只能使用鼠標右鍵。鼠標左鍵被保留用于單擊按鈕事件。

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

創(chuàng)建QDrag 對象。這個類提供了基于MIME的拖放數(shù)據(jù)傳輸?shù)闹С帧?/p>

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()方法中的代碼告訴我們,松開鼠標按鈕完成拖放操作。找出當(dāng)前鼠標指針位置并將按鈕移動到相應(yīng)的位置。

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

我們指定放置動作的類型。在當(dāng)前情況下,它是一個移動動作。

程序執(zhí)行后

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換

    TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換

    這篇文章主要介紹了TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • 最新評論