Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing custom widget through drag&drop
Forum Updated to NodeBB v4.3 + New Features

Passing custom widget through drag&drop

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 2.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ShinSat
    wrote on last edited by ShinSat
    #1

    Hi All,

    I have a quick question about QMimeData() in PyQt5.9.
    Is it possible to pass a custom widget like a custom QLabel/QPushButton in QMimeData through Drag&Drop? How can I setData() the widget information? Also it would be great if there's any alternative if not possible.
    I want to lay out the widget in different View(eg.Widget/QMainWindow).

    Thanks in advance for your update.
    Sat

    m.sueM 1 Reply Last reply
    0
    • S ShinSat

      Hi All,

      I have a quick question about QMimeData() in PyQt5.9.
      Is it possible to pass a custom widget like a custom QLabel/QPushButton in QMimeData through Drag&Drop? How can I setData() the widget information? Also it would be great if there's any alternative if not possible.
      I want to lay out the widget in different View(eg.Widget/QMainWindow).

      Thanks in advance for your update.
      Sat

      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by m.sue
      #2

      Hi @ShinSat

      If you just want to do it between different instances of your own program you can always serialize the object you want to drag, and de-serialize it at the drop. Which means you just send a string.

      -Michael.

      S 1 Reply Last reply
      0
      • m.sueM m.sue

        Hi @ShinSat

        If you just want to do it between different instances of your own program you can always serialize the object you want to drag, and de-serialize it at the drop. Which means you just send a string.

        -Michael.

        S Offline
        S Offline
        ShinSat
        wrote on last edited by
        #3

        @m.sue Thanks for an update!
        Is there any code snippet so that I can learn in practical codes?

        m.sueM 1 Reply Last reply
        0
        • S ShinSat

          @m.sue Thanks for an update!
          Is there any code snippet so that I can learn in practical codes?

          m.sueM Offline
          m.sueM Offline
          m.sue
          wrote on last edited by
          #4

          Hi @ShinSat

          In this example http://doc.qt.io/qt-5/qtwidgets-draganddrop-draggabletext-dragwidget-cpp.html you can see, they send text but do infact something with widgets.

          -Michael.

          S 1 Reply Last reply
          0
          • m.sueM m.sue

            Hi @ShinSat

            In this example http://doc.qt.io/qt-5/qtwidgets-draganddrop-draggabletext-dragwidget-cpp.html you can see, they send text but do infact something with widgets.

            -Michael.

            S Offline
            S Offline
            ShinSat
            wrote on last edited by ShinSat
            #5

            @m.sue I'm trying it in Python but no luck so far..

                        mime = QMimeData()
                        mime.setData('application/widget', QByteArray(QLabel('ABC')))
            

            Got the following errors. SO,,, there should be a trick that I'm missing...
            +++++++++++++++++++++++++++++++++++++++++++++++++
            mime.setData('application/widget', QByteArray(QLabel('ABC')))
            TypeError: arguments did not match any overloaded call:
            QByteArray(): too many arguments
            QByteArray(int, str): argument 1 has unexpected type 'QLabel'
            QByteArray(Union[QByteArray, bytes, bytearray]): argument 1 has unexpected type 'QLabel'
            +++++++++++++++++++++++++++++++++++++++++++++++++

            In addition, tried this.

                        mime = QMimeData()
                        itemData = QByteArray()
                        dataStream = QDataStream(itemData)
                        wgt = QWidget()
                        dataStream << wgt
                        mime.setData('application/widget', itemData)
            

            and got the followings.
            ++++++++++++++++++++++++++++++++++++++
            Traceback (most recent call last):
            File "/Users/kiaora/pyqt/dragAndDrop/examples/draganddrop_test.pyw", line 230, in mousePressEvent
            dataStream << wgt
            TypeError: unsupported operand type(s) for <<: 'QDataStream' and 'QWidget'
            ++++++++++++++++++++++++++++++++++++++

            Any ideas???

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              You can't pass around widget instances though QMimeData. You should rather serialise the properties you want from your widget and create a new one based on that at the target location. Otherwise, you can always try to implement a custom QMimeData class that does handle QWidget objects.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              1
              • SGaistS SGaist

                Hi,

                You can't pass around widget instances though QMimeData. You should rather serialise the properties you want from your widget and create a new one based on that at the target location. Otherwise, you can always try to implement a custom QMimeData class that does handle QWidget objects.

                S Offline
                S Offline
                ShinSat
                wrote on last edited by ShinSat
                #7

                Thank you very much for your help, @SGaist!
                Well, based on the discussion so far, a right way is either to create a custom QMimeData class or, I guess, place and hierarchically handle all widget on ONE mother base widget, which would enable to re-lay out them according to the requirement(s). But not easy, right?

                Thanks,
                Sat

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  No, like also suggested, you can serialise the properties of your widget, create a new one on the target that you setup with said properties and then delete the original.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  S 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    No, like also suggested, you can serialise the properties of your widget, create a new one on the target that you setup with said properties and then delete the original.

                    S Offline
                    S Offline
                    ShinSat
                    wrote on last edited by
                    #9

                    @SGaist The problem is the passing widget is a GUI with a lot of user inputs and app outputs ,not empty. You mean, I can copy and pass everything to reproduce it in the destination?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Give your widgets a proper interface that allows to retrieve their state so that you can restore them as needed.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved