PyQt5.QtWidgetss实现QPushButton多按钮的新增、拖动

4 篇文章 0 订阅
QtWidgetss之QPushButton的新增、拖动

本例程主要实现单击按钮触发添加新的按钮,添加的按钮可以实现自由拖动,并可以触发按钮单击事件
不足之处是,新添加的的按钮不能独立触发事件,有待改进…
程序运行效果如下:
请添加图片描述

  1. 点击添加按钮,添加新的按钮,试着拖动新增加假的按钮,如下图:

在这里插入图片描述
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_())
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值