QT 디자이너를 사용하여 LineEdit 위젯이 포함 된 xml-File을 만들었습니다. 스크립트에서 드래그 드롭을 통해 파일 경로를 방출하려고했습니다. 그것은 작동하지만 세인트. 잘못된 : URL은 두 번 방출과 같은됩니다Qt + pyqt가 삭제 된 URL을 두 번 냈습니다.
/D :/Qtfile : /// D :/Qt는
내가 PyQt event emmitted twice 같은 유사한 주제를 알고는 유래에서 설명합니다. 그러나 나는 나의 대답을 찾을 수 없다. 아마 나는 그것을 놓친다. 왜 두 번? 첫 번째 "file : //"이 사라진 이유는 무엇입니까?
Qt 디자이너를 사용하지 않고 클래스 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>
내가 사용했습니다 qurl :: 경로()와 qurl을() : toLocalFile(). 결과는 같습니다. 그러나 :: host()는 내가 원하는 문자열을 반환합니다 : file : /// D :/Qt. 나는 이해할 수 없다. 왜 path()가 그것을 돌려 준다. –