-
Notifications
You must be signed in to change notification settings - Fork 0
/
让控件支持拖拽动作.py
65 lines (51 loc) · 1.39 KB
/
让控件支持拖拽动作.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
让控件支持拖拽的动作
A。setDragEnabled(True)
B.setAcceptEvent(True)
B需要两个事件
1.dragEnterEvent 将A拖到B触发
2.dropEvent 将B的区域放下时触发
"""
"""
QPainter
painter=QPainter()
painter.begin()
painter.drawText(...)
painter.end()
固定格式
"""
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys,math
#编写一个类,继承QComboBox属性
class MyComboBox(QComboBox):
def __init__(self):
super().__init__()
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
print(e)
if e.mimeData().hasText():
e.accept()
else:
e.ignore()
def dropEvent(self,e):
self.addItem(e.mimeData().text())
#主类
class DrapDropDemo(QWidget):
def __init__(self):
super().__init__()
formLayout=QFormLayout()
formLayout.addRow(QLabel("请将左边的文字拖拽到右边下拉列表中"))
lineEdit=QLineEdit()
lineEdit.setDragEnabled(True) #让lineEdit可以拖动
#创建一个重新定义过的下拉列表
combo=MyComboBox()
#将两个控件添加到表单布局
formLayout.addRow(lineEdit,combo)
self.setLayout(formLayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = DrapDropDemo()
main.show()
sys.exit(app.exec_())