当前位置: 移动技术网 > IT编程>脚本编程>Python > PyQt5每天必学之拖放事件

PyQt5每天必学之拖放事件

2019年06月17日  | 移动技术网IT编程  | 我要评论

鬼屋寻女攻略,保定赶集,t131次列车

在pyqt5教程的这一部分,我们将讨论拖放操作。

在电脑图形用户界面,拖放事件就是点击一个虚拟对象,并将其拖动到其他位置或到另一个虚拟物体的动作。在一般情况下,它可以被用于调用多种动作,或创建两个抽象对象之间的关联的各种类型。

拖放事件是图形用户界面的一部分。拖放操作使用户能够直观地操作一些复杂的事情。

通常情况下,我们可以拖放两种类型:数据或某些图形对象。如果我们从一个应用程序拖动图像到另一个,我们拖放的是二进制数据。如果我们拖放firefox标签并将其移动到另一个地方,我们拖放的是图形组件。

简单拖放事件

在这个例子中,我们有一个qlineedit控件和一个qpushbutton控件。我们从单行文本编辑控件中将输入的文本选中后拖到按钮控件上后松开鼠标,按钮的标签将发生变化。

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

"""
pyqt5 教程

这是一个简单的拖放例子。

作者:我的世界你曾经来过
博客: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控件的一些方法重写。因此,我们创造我们自己的按钮类将从qpushbutton类继承。

 self.setacceptdrops(true)

为控件启用拖放事件。

def dragenterevent(self, e):
 if e.mimedata().hasformat('text/plain'):
  e.accept()
 else:
  e.ignore()

首先,重写了dragenterevent()方法。告知我们接受的数据类型(text/plain)。通常情况下,它是纯文本。

 def dropevent(self, e):
 self.settext(e.mimedata().text())

接下来重写了dropevent()方法,这里定义了drop事件将要做的事情。在这里我们改变按钮控件的文本。

edit = qlineedit('', self)
edit.setdragenabled(true)

若要启用qlineedit控件的拖动操作,需要做的是调用setdragenabled()方法来激活它。

程序执行后

这里写图片描述

拖放按钮控件

在下面的例子中,我们将演示如何拖放一个按钮控件。

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

"""
pyqt5 教程

在这个程序中,我们可以按上用鼠标左键点击或拖动一个按钮,用鼠标右键单击删除按钮。

作者:我的世界你曾经来过
博客: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' 消息打印到控制台。如果用鼠标右键按住按钮并移动鼠标,程序将执行一个拖放按钮控件事件。

class button(qpushbutton):

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

创建一个button 类从qpushbutton派生。我们还重写了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的拖放数据传输的支持。

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()方法中的代码告诉我们,松开鼠标按钮完成拖放操作。找出当前鼠标指针位置并将按钮移动到相应的位置。

e.setdropaction(qt.moveaction)
e.accept()

我们指定放置动作的类型。在当前情况下,它是一个移动动作。

程序执行后

这里写图片描述 这里写图片描述

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网