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

PyQt5實(shí)現(xiàn)拖放功能

 更新時(shí)間:2021年10月28日 17:17:48   作者:輸天半子  
這篇文章主要為大家詳細(xì)介紹了PyQt5實(shí)現(xiàn)拖放功能,拖放一個(gè)按鈕的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在這節(jié)教程中,我們將探討PyQt5中的拖放操作。

在計(jì)算機(jī)圖形用戶界面(GUI)中,拖放是在某個(gè)虛擬對(duì)象上點(diǎn)擊并拖動(dòng)到另一個(gè)位置或虛擬對(duì)象上的操作。它通常用于調(diào)用多個(gè)動(dòng)作,或?yàn)閮蓚€(gè)抽象對(duì)象創(chuàng)建某些聯(liá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,并通過將LineEdit中的文本拖放到按鈕上來改變按鈕的標(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類。

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ù)類型(在這里是普通文本)。

def dropEvent(self, e):

 self.setText(e.mimeData().text())

通過重新實(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)了拖放操作,可以通過鼠標(biāo)右擊進(jìn)行拖動(dòng)。

class Button(QPushButton):

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

我們從QPushButton派生了一個(gè)Button類,并重新實(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()方法中,我們要為松開鼠標(biāo)后的操作進(jìn)行編碼,并完成drop操作。即找出鼠標(biāo)指針的當(dāng)前位置,并將按鈕移動(dòng)過去。

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

我們定義了drop動(dòng)作的類型。這里是move動(dòng)作。

本節(jié)教程講解了拖放操作。

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

相關(guān)文章

最新評(píng)論