当前位置: 移动技术网 > IT编程>脚本编程>Python > PyQt5--ButtonDrag

PyQt5--ButtonDrag

2018年09月21日  | 移动技术网IT编程  | 我要评论

火影忍者香磷h,挽弓当挽强,2011年放假安排

 1 # -*- coding:utf-8 -*-
 2 '''
 3 created on sep 21, 2018
 4 
 5 @author: sashuangyibing
 6 
 7 comment: 
 8 '''
 9 import sys
10 from pyqt5.qtcore import qt,qmimedata
11 from pyqt5.qtgui import qdrag
12 from pyqt5.qtwidgets import qapplication,qwidget,qpushbutton
13 
14 class button(qpushbutton):
15     def __init__(self,title,parent):
16         super().__init__(title,parent)
17     
18     def mousemoveevent(self, e):
19         if e.buttons() != qt.rightbutton:
20             return
21         
22         mimedata = qmimedata()
23         drag = qdrag(self)
24         drag.setmimedata(mimedata)
25         drag.sethotspot(e.pos()-self.rect().topleft())
26         dropaction = drag.exec_(qt.moveaction)
27     
28     def mousepressevent(self, e):
29         qpushbutton.mousepressevent(self,e)
30         if e.button() == qt.leftbutton:
31             print ('press')
32             
33 class new_test(qwidget):
34     def __init__(self):
35         super().__init__()
36         self.initui()
37         
38     def initui(self):
39         self.setacceptdrops(true)
40         self.button = button('button',self)
41         self.button.move(100,65)
42         
43         self.setgeometry(300,300,280,150)
44         self.setwindowtitle('click or move')
45         self.show()
46     
47     def dragenterevent(self, e):
48         e.accept()
49         
50     def dropevent(self, e):
51         position = e.pos()
52         self.button.move(position)
53         
54         e.setdropaction(qt.moveaction)
55         e.accept()
56 
57 if __name__ == '__main__':
58     app = qapplication(sys.argv)
59     ex = new_test()
60     sys.exit(app.exec_())
61         
62         
63         

 

在button上右键拖动时,按钮会跟随鼠标移动,如果是在button左键时,会在控制台上打印出 "press"

 

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

相关文章:

验证码:
移动技术网