PyQt5.QtWidgetss实现QPushButton多按钮的新增、拖动
程序员文章站
2023-10-10 16:32:46
PushButton多按钮添加与拖动实现本例程主要实现单击按钮触发添加新的按钮,添加的按钮可以实现*拖动,并可以触发按钮单击事件不足之处是,新添加的的按钮不能独立触发事件,有待改进…实现的效果:第一步、创建基本QApplication对象的显示# -*- coding: utf-8 -*-import sysfrom PyQt5.QtWidgets import *from PyQt5.QtCore import Qt,QMimeData,QPointfrom PyQt5.QtGui i...
QtWidgetss之QPushButton的新增、拖动
本例程主要实现单击按钮触发添加新的按钮,添加的按钮可以实现*拖动,并可以触发按钮单击事件
不足之处是,新添加的的按钮不能独立触发事件,有待改进…
程序运行效果如下:
- 点击添加按钮,添加新的按钮,试着拖动新增加假的按钮,如下图:
2. 鼠标点击新增的按钮,触发打印事件:
第一步、创建基本QApplication对象的显示
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt,QMimeData,QPoint
from PyQt5.QtGui import QDrag
class ui(QWidget):
def __init__(self):
super().__init__()
#self.show_ui()
self.setWindowTitle("Add Button Demo")
self.setGeometry(100,100,680,450)#对象放置在坐标(100,100);长,宽680*450
if __name__=='__main__':
app = QApplication(sys.argv)#创建对象
ui = ui()
ui.show()
sys.exit(app.exec_())
第二步、创建按钮类:
class Button(QPushButton):
def __init__(self,title,parent):
super().__init__(title,parent)
def mouseMoveEvent(self,e):
if e.buttons() !=Qt.LeftButton:#鼠标左击
print('')#注意
mimeData = QMimeData()#创建鼠标位置信息对象
drag = QDrag(self)
drag.setMimeData(mimeData)#设置拖拽数据
drag.setHotSpot(e.pos() - self.rect().topLeft())#设置拖动过程中鼠标在相对拖动对象的位置
dropAction = drag.exec_(Qt.MoveAction)
第三步、在show_ui函数中添加一个普通的按钮和一个自定义的按钮
def show_ui(self,y=0):
self.setAcceptDrops(True)
self.addBtn = QPushButton(self)#普通按钮
self.addBtn.setText('添加按钮')
self.addBtn.move(10,20)
self.button0 = Button('移动按钮',self)#自定义按钮
self.button0.move(10,50)
第四步、给第一个普通按钮触发添加事件,并定义触发函数
self.addBtn.clicked.connect(self.appendBtn)#添加触发click事件,触发添加新按钮函数
#添加可拖拽移动按钮函数
def appendBtn(self,e):
global index
index = index +1
self.button1 = Button("移动按钮"+str(index),self)
self.button1.setGeometry(100,index*50,80,40)
self.button1.setVisible(True) #按钮可视
print("btn ok")#打印用于监控
第五步、拖放函数的添加,为新增加的按钮也添加触发事件
(有待解决不同按钮独立触发)
def dragEnterEvent(self,e):
e.accept()
def dropEvent(self,e):
position = e.pos()#获取鼠标位置数据
if self.button1.isDown():
print("case1")
self.button1.move(position)#写入对象位置信息
self.button1.clicked.connect(self.btn1_case)#为按钮添加click触发事件
e.setDropAction(Qt.MoveAction)
e.accept()
全部代码如下:
# -*- coding: utf-8 -*-
import sys,time
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt,QMimeData,QPoint
from PyQt5.QtGui import QDrag
index = 0
class Button(QPushButton):
def __init__(self,title,parent):
super().__init__(title,parent)
def mouseMoveEvent(self,e):
if e.buttons() !=Qt.LeftButton:#鼠标左击
print('')
mimeData = QMimeData()
drag = QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft())
dropAction = drag.exec_(Qt.MoveAction)
class ui(QWidget):
def __init__(self):
super().__init__()
self.show_ui()
self.setWindowTitle("Add Button Demo")
self.setGeometry(100,100,680,450)
def printstr(self,e):
print("printstr")
def show_ui(self,y=0):
self.setAcceptDrops(True)
#self.addBtn.setText('添加按钮')
self.addBtn = QPushButton(self)
self.addBtn.setText('添加按钮')
self.addBtn.move(10,20)
self.addBtn.clicked.connect(self.appendBtn)#触发添加按钮
self.button0 = Button('移动按钮',self)
self.button0.move(10,50)
self.button0.clicked.connect(self.btn0_case)
def mousePressEvent(self,e):
print("鼠标按下事件")
def dragEnterEvent(self,e):
e.accept()
def dropEvent(self,e):
position = e.pos()#获取鼠标位置数据
print("in this")
if self.button0.isDown():
print("case0")
self.button0.move(position)
if self.button1.isDown():
print("case1")
self.button1.move(position)
self.button1.clicked.connect(self.btn1_case)
e.setDropAction(Qt.MoveAction)
e.accept()
def appendBtn(self,e):
global index
index = index +1
self.button1 = Button("移动按钮"+str(index),self)
self.button1.setGeometry(100,index*50,80,40)
self.button1.setVisible(True)
print("btn ok")
def btn0_case(self,e):
print("Trigger btn0_case: ")
def btn1_case(self,e):
print("Trigger btn1_case: ")
if __name__=='__main__':
app = QApplication(sys.argv)
ui = ui()
ui.show()
sys.exit(app.exec_())
本文地址:https://blog.csdn.net/m0_49047167/article/details/107059383