赞
踩
目录
- '''
- 选择文本输入框中的文本,移动到下拉框中自动添加
- 步骤:
- 1、将文本输入框设置为可拖拽
- lineEdit.setDragEnabled(True)
- 2、设置下拉框的两个信号属性:
- 0)将控件设置为接受其它地方拖拽过来的内容
- self.setAcceptDrops(True)
- 1)拖动还没有放下时触发
- def dragEnterEvent(self, event):
- print(event)
- ## 若拖拽含有文本,则接受,否则忽略
- if event.mimeData().hasText():
- event.accept()
- else:
- event.ignore()
- 2)拖动放下时触发
- def dropEvent(self,event):
- self.addItem(event.mimeData().text())
- '''
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # @Time : 2022/5/26 16:46
- # @Author : @linlianqin
- # @Site :
- # @File : Drag_learn.py
- # @Software: PyCharm
- # @description:
-
- '''
- 选择文本输入框中的文本,移动到下拉框中自动添加
- 步骤:
- 1、将文本输入框设置为可拖拽
- 2、设置下拉框的两个信号属性:
- 0)将控件设置为接受其它地方拖拽过来的内容
- 1)拖动还没有放下时触发
- 2)拖动放下时触发
- '''
- from PyQt5.QtWidgets import QComboBox,QWidget,QLineEdit,QFormLayout,QLabel,QApplication
- import sys
-
- class Mycombobox(QComboBox):
- def __init__(self):
- super(Mycombobox, self).__init__()
- # 0)将控件设置为接受其它地方拖拽过来的内容
- self.setAcceptDrops(True)
-
- # 1)拖动还没有放下时触发
- def dragEnterEvent(self, event):
- print(event)
- ## 若拖拽含有文本,则接受,否则忽略
- if event.mimeData().hasText():
- event.accept()
- else:
- event.ignore()
-
- # 2)拖动放下时触发
- def dropEvent(self,event):
- self.addItem(event.mimeData().text())
-
- class DragDropDemo(QWidget):
- def __init__(self):
- super(DragDropDemo, self).__init__()
- layout = QFormLayout()
- layout.addRow(QLabel("请将左边文本移动到右边下拉框中"))
- combo = Mycombobox()
- lineEdit = QLineEdit()
- layout.addRow(lineEdit,combo)
-
- # 1、将文本输入框设置为可拖拽
- lineEdit.setDragEnabled(True)
-
- self.setLayout(layout)
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- mainWin = DragDropDemo()
- mainWin.show()
- sys.exit(app.exec_())
关键代码
# 将文本复制到剪切板 clipboard = QApplication.clipboard() clipboard.setText(self.textlabel1.text())# 获取剪切板的文本
clipboard.text()# 将图像复制到剪切板
clipboard = QApplication.clipboard() clipboard.setPixmap(QPixmap("icon.jpg"))# 获取剪切板上的图像
clipboard.pixmap()
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # @Time : 2022/5/26 17:00
- # @Author : @linlianqin
- # @Site :
- # @File : ClipBoard_learn.py
- # @Software: PyCharm
- # @description:
-
- '''
- 使用剪切板
- '''
- from PyQt5.QtWidgets import QComboBox,QWidget,QPushButton,QLabel,QGridLayout,QApplication
- from PyQt5.QtGui import QPixmap
- import sys
-
- class ClipBoard_learn(QWidget):
- def __init__(self):
- super(ClipBoard_learn, self).__init__()
-
- textcopyBtn = QPushButton("复制text")
- textpasteBtn = QPushButton("粘贴text")
-
- imagecopyBtn = QPushButton("复制image")
- imagepasteBtn = QPushButton("粘贴image")
-
- self.textlabel1 = QLabel("文本")
- self.textlabel2 = QLabel()
- self.imageLabel1 = QLabel()
- self.imageLabel2 = QLabel()
-
- gridLayout = QGridLayout()
- gridLayout.addWidget(textcopyBtn,0,0)
- gridLayout.addWidget(textpasteBtn,0,1)
- gridLayout.addWidget(imagepasteBtn,1,1)
- gridLayout.addWidget(imagecopyBtn,1,0)
- gridLayout.addWidget(self.textlabel1,2,0)
- gridLayout.addWidget(self.textlabel2,2,1)
- gridLayout.addWidget(self.imageLabel1,3,0)
- gridLayout.addWidget(self.imageLabel2,3,1)
-
- self.setLayout(gridLayout)
- self.setWindowTitle("剪切板演示")
-
- textcopyBtn.clicked.connect(self.textcopy)
- textpasteBtn.clicked.connect(self.textpaste)
- imagecopyBtn.clicked.connect(self.imagecopy)
- imagepasteBtn.clicked.connect(self.imagepaste)
-
- def textcopy(self):
- clipboard = QApplication.clipboard()
- clipboard.setText(self.textlabel1.text())
-
- def textpaste(self):
- clipboard = QApplication.clipboard()
- self.textlabel2.setText(clipboard.text())
-
- def imagecopy(self):
- clipboard = QApplication.clipboard()
- clipboard.setPixmap(QPixmap("icon.jpg"))
-
- def imagepaste(self):
- clipboard = QApplication.clipboard()
- self.imageLabel2.setPixmap(clipboard.pixmap())
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- mainWin = ClipBoard_learn()
- mainWin.show()
- sys.exit(app.exec_())
复制粘贴文本演示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。