def eventFilter(self, source: QLineEdit, event: Union[QDropEvent, QMouseEvent, QDragEnterEvent]): if event.type() == QEvent.DragEnter: event.accept() elif event.type() == QEvent.Drop: # 任何拖放事件 都先清空原来的内容 source.clear() md = event.mimeData() # type: QMimeData if md.hasUrls() and len(md.urls()) == 1: path = md.urls()[0].toLocalFile() if os.path.exists(path): source.setText(path) self.update_name.emit(path) return True elif event.type() == QEvent.MouseButtonDblClick: # 选择本地headers.json文件 fileName, selectedFilter = QFileDialog.getOpenFileUrl( None, caption="select headers.json", dir=QUrl("file://.")) if isinstance(fileName, QUrl): path = fileName.toLocalFile() if os.path.exists(path): source.clear() source.setText(path) self.update_name.emit(path) return True return super(HeaderFileQLineEdit, self).eventFilter(source, event)
def select_header_file(self): ''' 通过点击select按钮选择headers.json ''' fileName, selectedFilter = QFileDialog.getOpenFileUrl(None, caption="select headers.json", dir=QUrl("file://.")) if isinstance(fileName, QUrl): path = fileName.toLocalFile() if os.path.exists(path): self.lineEdit_headers_path.clear() self.lineEdit_headers_path.setText(path) self.load_from_file()
def eventFilter(self, source: QLineEdit, event: Union[QDropEvent, QMouseEvent, QDragEnterEvent]): if event.type() == QEvent.DragEnter: event.accept() elif event.type() == QEvent.Drop: # 任何拖放事件 都先清空原来的内容 source.clear() md = event.mimeData() # type: QMimeData if md.hasUrls() and len(md.urls()) == 1: path = md.urls()[0].toLocalFile() if os.path.exists(path): source.setText(path) self.update_name.emit(path) return True elif event.type() == QEvent.MouseButtonDblClick: # 获取剪切板内容 看看有没有链接或者是文件 clipboard = QApplication.clipboard() md = clipboard.mimeData() if md.hasUrls() and len(md.urls()) == 1: # 比如复制了文件 这里就可以获取到文件的路径 但超链接并不会在这里 path = md.urls()[0].toLocalFile() if os.path.exists(path): source.clear() source.setText(path) self.update_name.emit(path) elif md.hasText(): # 检查是不是http(s)://开头的链接 text = md.text() if re.match(r'^https?://', text): source.clear() source.setText(text) self.update_name.emit(text) else: # 否则尝试选择本地元数据文件 fileName, selectedFilter = QFileDialog.getOpenFileUrl( None, caption="选择元数据文件", dir=QUrl("file://.")) if isinstance(fileName, QUrl): path = fileName.toLocalFile() if os.path.exists(path): source.clear() source.setText(path) self.update_name.emit(path) return True return super(URIQLineEdit, self).eventFilter(source, event)