def eventFilter(self, obj: QObject, event: QEvent): """ Implementation of QObject::eventFilter() Intercept drag-and-drop events before they are passed to the QTableWidget. In response to the drop of a text (.txt or .csv) file, opens that file. @param obj: the "watched object" that events are intercepted from (self in our case) @param event: the event @return: whether the event is accepted or rejected for further processing. """ if event.type() == QEvent.Drop: event.accept() mime = event.mimeData() if mime.hasText(): file_path = mime.text() if file_path.endswith('.txt') or file_path.endswith('.csv'): file_path = file_path.replace('file:///', '') with open(file_path) as reader: rows = reader.readlines() if rows: self.set_row_data(rows) self.parent.setWindowTitle(os.path.basename(file_path)) return True elif event.type() == QEvent.DragEnter: event.accept() return True return False
def dropEvent(self, event: QtCore.QEvent) -> None: """Event is called after dragEnterEvent.""" for url in event.mimeData().urls(): # pragma: no cover self.url = url filename = self.url.path() if os.path.isfile(filename): try: self.add_mesh(pyvista.read(filename)) except IOError as exception: print(str(exception)) return None