qt + pyqt发出两次丢弃的URL

时间:2017-02-23 13:07:05

标签: qt python-3.x drag-and-drop pyqt5

我已经使用QT-Designer创建了一个包含LineEdit小部件的xml-File。在脚本中,我试图通过拖放来发出文件路径。它工作但圣。是错误的:网址会被发出两次,看起来像是:

/ d:/ Qtfile:/// d:/ Qt的

我知道在stackoverflow中讨论了类似PyQt event emmitted twice之类的主题。但我找不到答案......也许我想念它。为什么两次?为什么第一个"文件://"消失了?

如果我不使用Qt-Designer并定义一个SubClass来拖放类似CustomEditLine(QLineEdit)类的文本:...然后在主窗口中创建QlineEdit的实例,url将只发出一次但是仍然" / D:/ Qt"。 这是我的代码:

from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import QObject,QEvent
import sys

qtCreatorFile=".\\gui\\testdrop.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class QDropHandler(QObject):
    def __init__(self, parent=None):
        super(QDropHandler, self).__init__(parent) 

    def eventFilter(self, obj, event):
        if event.type() == QEvent.DragEnter:
            event.accept()
        if event.type() == QEvent.Drop:
            md = event.mimeData()
            if md.hasUrls():
                for url in md.urls():                   
                    obj.setText(url.path())
                    break
            event.accept()
        return False

class root_App(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(root_App, self).__init__()
        self.setupUi(self)
        self.lineEdit_1.installEventFilter(QDropHandler(self))

if __name__=="__main__":
    app= QtWidgets.QApplication(sys.argv)
    window=root_App()
    window.show()
    sys.exit(app.exec_())

和我的ui-xml:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>758</width>
    <height>424</height>
   </rect>
  </property>
  <property name="acceptDrops">
   <bool>true</bool>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QLineEdit" name="lineEdit_1">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>140</y>
     <width>691</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

0 个答案:

没有答案