从lineEdit拖放到PyQt5中的按钮

时间:2019-06-16 14:47:57

标签: python-3.x pyqt5

我想从lineEdit小部件中拖动纯文本以在PyQt5中放置pushbutton标签。这是代码:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(873, 663)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton2= QtWidgets.QPushButton(self.centralwidget)
        self.pushButton2.setGeometry(QtCore.QRect(280, 40, 93, 28))
        self.pushButton2.setObjectName("pushButton2")
        self.pushButton2.setAcceptDrops(True)
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(120, 200, 113, 22))
        self.lineEdit.setObjectName("lineEdit")
        MainWindow.setCentralWidget(self.centralwidget)



        self.lineEdit.setDragEnabled(True)
        self.retranslateUi(MainWindow)

    class pushButton2(QtWidgets.QPushButton):
        def __init__(self, title, parent):
            super().__init__(title, parent)   
            self.setAcceptDrops(True)   

        def dragEnterEvent(self, event):

            if event.mimeData().hasFormat('text/plain'):
                event.accept()
            else:
                event.ignore() 

        def dropEvent(self, event):

            self.setText(event.mimeData().text())

    def retranslateUi(self, MainWindow):

        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

        self.pushButton2.setText(_translate("MainWindow", "PushButton2"))
        self.lineEdit.setText(_translate("MainWindow", "k"))

if __name__ == "__main__":


    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance() 



    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec_()

我希望从lineEdit中拖动文本以显示为按钮标签,但未接受,为什么?

1 个答案:

答案 0 :(得分:0)

尝试一下:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


#    class pushButton2(QtWidgets.QPushButton):
class PushButton(QtWidgets.QPushButton):                                  # <---
    def __init__(self, title, parent):
        super().__init__(title, parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        if e.mimeData().hasFormat('text/plain'):
            e.accept()
        else:
            e.ignore() 

    def dropEvent(self, e):
        self.setText(e.mimeData().text()) 


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(873, 663)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

#        self.pushButton2= QtWidgets.QPushButton(self.centralwidget)
        self.pushButton2 = PushButton("PushButton2", self.centralwidget)   # <---

        self.pushButton2.setGeometry(QtCore.QRect(120, 140, 93, 28))
        self.pushButton2.setObjectName("pushButton2")
#        self.pushButton2.setAcceptDrops(True)

        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(120, 200, 113, 22))
        self.lineEdit.setObjectName("lineEdit")
        MainWindow.setCentralWidget(self.centralwidget)

        self.lineEdit.setDragEnabled(True)
        self.retranslateUi(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton2.setText(_translate("MainWindow", "PushButton2"))
        self.lineEdit.setText(_translate("MainWindow", "k"))


class ExampleApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)


if __name__ == "__main__":
    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance() 
    MainWindow = ExampleApp()
    MainWindow.show()
    app.exec_()

enter image description here