Я использую pyqt5 и у меня проблемы с моим кодом. Я хочу использовать перетаскивание, но я не могу использовать dragEnterEvent и dropEvent, так как в моем коде много функций. Я попробовал несколько способов вызвать эти функции, но не смог.
Вот мой код:
from PyQt5 import QtWidgets, uic, QtGui, QtCore
from PyQt5.QtCore import *
from PyQt5.QtCore import QEvent
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import *
import sys
class Button(QPushButton):
def mouseMoveEvent(self, e):
if event.buttons() != QtCore.Qt.LeftButton:
return
mimeData = QMimeData()
drag = QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(self.mousePos - self.rect().topLeft())
drag.Action = drag.exec_(Qt.MoveAction)
def mousePressEvent(self, e):
super().mousePressEvent(e)
class ProgramMaker(QWidget):
def __init__(self):
super(ProgramMaker, self).__init__()
self.interface = uic.loadUi('WindowPM.ui')
self.interface.setFixedSize(self.interface.size());
self.interface.setWindowTitle('Program Maker')
self.interface1 = uic.loadUi('WindowPMProjectConfig.ui')
self.interface1.setFixedSize(self.interface1.size());
self.interface2 = uic.loadUi('WindowProject.ui')
self.interface2.setFixedSize(self.interface2.size());
self.interface.newproject.clicked.connect(self.NewProject)
self.frame=QtWidgets.QFrame()
self.interface.newproject.setToolTip('create new project')
self.interface1.buttonBox.accepted.connect(self.Project)
self.loading = uic.loadUi('WindowLoading.ui')
self.interface3 = uic.loadUi('WindowLayout.ui')
self.interface3.setFixedSize(self.interface3.size());
self.interface3.toolButton.clicked.connect(self.toolButtonAnimation)
self.interface3.toolButton.setMouseTracking(True)
self.interface4 = uic.loadUi('WindowWidget.ui')
self.interface2.createcommand.clicked.connect(self.CreateCommand)
self.animtoolgui = QPropertyAnimation(self.interface3.toolsgui, b"geometry")
self.animwin = QPropertyAnimation(self.interface3.window, b"geometry")
self.animcb = QPropertyAnimation(self.interface3.CreateButton, b"geometry")
self.animtoolbutton = QPropertyAnimation(self.interface3.toolButton, b"geometry")
self.animWidget = QPropertyAnimation(self.interface3.widget, b"geometry")
self.animbutton = QPropertyAnimation(self.interface3.pushButton, b'geometry')
self.animPushButtons = QPropertyAnimation(self.interface3.PushButtons, b'geometry')
self.interface.show()
def createbutton(self):
self.interface4.setAcceptDrops(True)
self.button = Button('Button', self.interface4)
self.button.move(160, 120)
self.button.show()
def leaveEvent2(self):
self.animtoolgui.setDuration(100)
self.animtoolgui.setStartValue(QRect(760, 0, 251, 641))
self.animtoolgui.setEndValue(QRect(960, 0, 41, 641))
self.animtoolgui.start()
self.animcb.setDuration(100)
self.animcb.setStartValue(QRect(800, 240, 100, 25))
self.animcb.setEndValue(QRect(960, 240, 61, 23))
self.animcb.start()
self.interface3.toolButton.clicked.connect(self.toolButtonAnimation)
def leaveEvent1(self):
self.animwin.setDuration(100)
self.animwin.setStartValue(QRect(20, 80, 140, 23))
self.animwin.setEndValue(QRect(-30, 60, 31, 23))
self.animwin.start()
self.interface3.toolButton.clicked.connect(self.guibuttons)
def leaveEvent(self):
#animtoolgui.setDuration(100)
#animtoolgui.setStartValue(QRect(760, 0, 251, 641))
#animtoolgui.setEndValue(QRect(960, 0, 41, 641))
#animtoolgui.start()
#animcb.setDuration(100)
#animcb.setStartValue(QRect(800, 240, 100, 25))
#animcb.setEndValue(QRect(960, 240, 61, 23))
#animcb.start()
self.animtoolbutton.setDuration(100)
self.animtoolbutton.setStartValue(QRect(120, 0, 41, 41))
self.animtoolbutton.setEndValue(QRect(0, 0, 41, 41))
self.animtoolbutton.start()
self.animWidget.setDuration(100)
self.animWidget.setStartValue(QRect(-20, 40, 180, 401))
self.animWidget.setEndValue(QRect(-20, 40, 16, 401))
self.animWidget.start()
#animwin.setDuration(100)
#animwin.setStartValue(QRect(20, 80, 140, 23))
#animwin.setEndValue(QRect(-30, 60, 31, 23))
#animwin.start()
self.animbutton.setDuration(100)
self.animbutton.setStartValue(QRect(0, 40, 160, 40))
self.animbutton.setEndValue(QRect(-20, 40, 16, 23))
self.animbutton.start()
self.interface3.toolButton.clicked.connect(self.toolButtonAnimation)
def CreateWindow(self):
self.interface4.show()
self.animtoolgui.setDuration(100)
self.animtoolgui.setStartValue(QRect(960, 0, 41, 641))
self.animtoolgui.setEndValue(QRect(760, 0, 251, 641))
self.animtoolgui.start()
self.animPushButtons.setDuration(100)
self.animPushButtons.setStartValue(QRect(970, 210, 47, 13))
self.animPushButtons.setEndValue(QRect(760, 220, 200, 20))
self.animPushButtons.start()
self.animcb.setDuration(100)
self.animcb.setStartValue(QRect(960, 240, 61, 23))
self.animcb.setEndValue(QRect(760, 240, 200, 30))
self.animcb.start()
self.interface3.toolButton.clicked.connect(self.leaveEvent2)
self.interface3.CreateButton.clicked.connect(self.createbutton)
def guibuttons(self):
self.animwin.setDuration(100)
self.animwin.setStartValue(QRect(-30, 60, 31, 23))
self.animwin.setEndValue(QRect(20, 80, 140, 23))
self.animwin.start()
self.interface3.toolButton.clicked.connect(self.leaveEvent1)
def toolButtonAnimation(self):
self.animtoolbutton.setDuration(100)
self.animtoolbutton.setStartValue(QRect(0, 0, 41, 41))
self.animtoolbutton.setEndValue(QRect(120, 0, 41, 41))
self.animtoolbutton.start()
self.animWidget.setDuration(100)
self.animWidget.setStartValue(QRect(-20, 40, 16, 401))
self.animWidget.setEndValue(QRect(-20, 40, 180, 401))
self.animWidget.start()
self.animbutton.setDuration(100)
self.animbutton.setStartValue(QRect(-20, 40, 16, 23))
self.animbutton.setEndValue(QRect(0, 40, 160, 40))
self.animbutton.start()
self.interface3.pushButton.clicked.connect(self.guibuttons)
self.interface3.window.clicked.connect(self.CreateWindow)
self.interface3.toolButton.clicked.connect(self.leaveEvent)
def CreateCommand(self):
self.interface3.show()
def Project(self):
self.interface1.destroy()
self.interface2.label_2.setText('' + self.interface1.lineEdit.text())
self.interface2.show()
def NewProject(self):
self.interface.destroy()
self.interface1.show()
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
position = e.pos()
self.button.move(position)
e.setDropAction(Qt.MoveAction)
e.accept()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
PM = ProgramMaker()
app.exec_()