If you already have pickle (get/set state) and deepcopy methods, how do you convert to QByteArray?

  • This is regarding useage of PyQt5. Not C++

    I'm implementing drag and drop for my Watermelon tool. It will suport DnD for almost all widgets appearing in the app.

    Easy enough though since each of my widget classes has been pickled / unpickled and deepcopied successfully. Thus, is there an easy way to convert the pickle data into QByteArray for the purpose of sticking in QMimeData?


  • I think you do:

    import _pickle as pickle

    obj = MyWidget()

    data = pickle.dumps(obj)
    byte_arr = QByteArray()
    data_stream = QDataStream(byte_array, QIODevice.WriteOnly)
    data_stream << data

    Also try passing in data directly to QByteArray() ctor.

  • It's as easy as this:

    One one end:

                        item_data = QByteArray(dill.dumps(child))
                        mime_data = QMimeData()
                        mime_data.setData(self.mime_type, item_data)

    On the other end:

               data = event.mimeData().data(self.mime_type).data()
               drop = dill.loads(data)

Log in to reply