0

Im trying to implement drag and drop to a table widget and getting an error Trying to construct an instance of an invalid type, type id: 793994105. There's multiple lines of this but with different ids. How can i get this working without errors so that I can see the print statement when I drag and drop a file eg pdf?

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(353, 320)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(48, 50, 256, 192))
        self.tableWidget.setAcceptDrops(True)
        self.tableWidget.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
        self.tableWidget.setDefaultDropAction(QtCore.Qt.CopyAction)
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setRowCount(0)

        types = ['text/uri-list']
        types.extend(self.tableWidget.mimeTypes())
        self.tableWidget.mimeTypes = lambda: types

        MainWindow.setCentralWidget(self.centralwidget)
        self.tableWidget.viewport().installEventFilter(self.tableWidget)


    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.Drop and
            event.mimeData().hasUrls()):
            print('Dropped')
            return True
        return super().eventFilter(source, event)

    
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Window = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(Window)
    Window.show()
    sys.exit(app.exec_())

I already tried implementing these drag and drop events with no luck too:

def dragEnterEvent(self, event):
    print('dragged')

def dropEvent(self, event):
    print('dropped')

Full error is:

Trying to construct an instance of an invalid type, type id: 793994105
Trying to construct an instance of an invalid type, type id: 793011571
Trying to construct an instance of an invalid type, type id: 53299009 
Trying to construct an instance of an invalid type, type id: 339825483
Trying to construct an instance of an invalid type, type id: 846557999
Trying to construct an instance of an invalid type, type id: 594112103
Trying to construct an instance of an invalid type, type id: 694380910
Trying to construct an instance of an invalid type, type id: 863265134
Trying to construct an instance of an invalid type, type id: 842025326
Trying to construct an instance of an invalid type, type id: 858735433
Trying to construct an instance of an invalid type, type id: 891630128
Trying to construct an instance of an invalid type, type id: 624046145
Trying to construct an instance of an invalid type, type id: 610667786
West
  • 2,350
  • 5
  • 31
  • 67
  • Please post 1st full stacktrace. – CristiFati Aug 23 '22 at 10:37
  • @CristiFati I've posted – West Aug 23 '22 at 10:39
  • Not very familiar with the *PyQt* wrapper, but why using *Ui\_MainWindow* (is it autogenerated?) instead of extending *QtWidgets.QMainWindow*? Also, do you have an *.ui* file? https://medium.com/swlh/python-pyqt5-build-a-simple-gui-with-no-effort-d51026620947. – CristiFati Aug 23 '22 at 10:43
  • Don't modify pyuic files, it's a bad practice and almost always results in issues due to misunderstanding how those classes should be used. Follow the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). Also, installing the event filter of the table for its viewport doesn't make any sense. – musicamante Aug 23 '22 at 10:47

0 Answers0