Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Drag an Item to another application (Solved)

    QML and Qt Quick
    2
    10
    4858
    Loading More Posts
    • 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.
    • T
      Tân Ngọc Đỗ last edited by

      Hello,

      I want to drag an Item, for ex: a rectangle from my window and drop it in Microsoft word or odt file. When finish dropping , the document will have the image of that rectangle or some information of the rectangle (color, width, height.....) Is is possible to do that?

      Thank you!

      1 Reply Last reply Reply Quote 0
      • O
        onek24 last edited by

        Hello,

        interesting idea, looks possible for me if you also use Cpp for that. I'll see what i can find out.

        1 Reply Last reply Reply Quote 0
        • T
          Tân Ngọc Đỗ last edited by

          Hello, Is QML enough to do that or I have to use C++ ?, until now I don't have any idea about it? Could you give me some suggestion?

          1 Reply Last reply Reply Quote 0
          • O
            onek24 last edited by

            I would say that you'll need C++. I would do it that way:

            QML: Start dragging, if dropped out of window: continue

            Cpp: Detect whether there is an useable Application or not, if yes: continue

            Cpp-QML: Get information about the QML Component and pass it to Cpp.

            Cpp: Copy the retrieved information to clipboard, set the windows focus to the application window and paste it in there using Cpp.

            I've never done that, it's just an idea. For pasting i would go ahead and use ring 0 to manipulate the data in your office application, if you are familiar with it. By manipulation i mean pasting the retrieved information.

            1 Reply Last reply Reply Quote 0
            • T
              Tân Ngọc Đỗ last edited by

              Thanks for your guide, maybe I will try and if I have any concern I will ask you.

              Thank you!

              1 Reply Last reply Reply Quote 0
              • O
                onek24 last edited by

                You're welcome, well i am developing since 5 months, thereof 3 with QML, so it may be that i can not help you further with it, but i will try to.

                1 Reply Last reply Reply Quote 0
                • T
                  Tân Ngọc Đỗ last edited by

                  Hello,

                  I tried to use a new appoach. Since Qt 5.2 support for drag and drop external application (I hope so) . I use this code piece to drag some text from text editor to QML application, it's ok, but when I drag a word from QML APP to text editor, my program is crash:

                  @
                  Rectangle {
                  id: background;
                  color: "white";
                  width: 300;
                  height: 300;

                  DropArea {
                      anchors.fill: parent
                      keys: ["text/plain"]
                      onEntered: {
                          //item.color = "#FCC"
                      }
                      onExited: {
                         // item.color = "#EEE"
                      }
                      onDropped: {
                       //   item.color = "#EEE"
                          if (drop.hasText) {
                              if (drop.proposedAction == Qt.MoveAction || drop.proposedAction == Qt.CopyAction) {
                                  txtDrag.text = drop.text
                                  drop.acceptProposedAction()
                                  print("dragsource =",drag.source)
                              }
                          }
                      }
                  }
                  
                  Text {
                      id: txtDrag
                      text: qsTr("Hello World")
                      Drag.active: mouseArea.drag.active
                      Drag.dragType: Drag.Automatic
                      Drag.mimeData: { "text/plain": txtDrag.text }
                      Drag.proposedAction: Qt.CopyAction
                      Drag.supportedActions: Qt.CopyAction
                       Drag.keys: ["text/plain"]
                  
                      Drag.onDragStarted : {
                          print("Start drag:")
                           //Drag.startDrag(Qt.CopyAction);
                      }
                        Drag.onDragFinished: {
                            //action.acceptProposedAction();
                          print("Finish Dragging");
                            Drag.drop();
                      }
                        MouseArea {
                            id:mouseArea;
                            anchors.fill: parent
                            drag.target: txtDrag
                        }
                  
                  }
                  

                  @

                  1 Reply Last reply Reply Quote 0
                  • O
                    onek24 last edited by

                    Do you have any console/debug output? And did your drag managed to finish or does it crash after it startet draging?

                    1 Reply Last reply Reply Quote 0
                    • T
                      Tân Ngọc Đỗ last edited by

                      Hello,
                      After I finish dropping the text to text editor : my program crash, if I drop the text inside QML app: it still run. I also try using C++ widget to drop outside the program and it's very easy, we just set mimedata for QDrag and the text editor will receive our dragging data. But I still want to try with QML. I refer this example and It also crash : http://qt-project.org/doc/qt-5/qtquick-externaldraganddrop-example.html

                      1 Reply Last reply Reply Quote 0
                      • T
                        Tân Ngọc Đỗ last edited by

                        Until now, I don't know how to use drag and drop to external program using QML ; but we can incorporate with C++. For ex:
                        in C++:
                        @class A :public QObject {//remember to register A to be used in QML
                        Q_OBJECT
                        Q_INVOKABLE void DraggingProcessing(QObject *rect) {
                        QString strWidth = rect->property( "width" ).toString();
                        QString strHeight = rect->property( "height" ).toString();

                        QDrag * drag = new QDrag( rect );
                        QMimeData* mime = new QMimeData;
                        // a custom mime type so that no other app would ever accept it
                        mime->setText( strWidth + " " + strHeight );
                        drag->setMimeData( mime );
                        QPixmap pix;
                        //get pixmap here
                        drag->setPixmap( pix );//to set the image while dragging

                        drag->exec( Qt::CopyAction );
                        }
                        in QML:
                        A {
                        id: a
                        }

                        Rectangle {
                        id: rect
                        width:100
                        height:100
                        MouseArea {
                        id:mouseArea
                        anchors.fill: parent
                        onPositionChanged : a.DraggingProcessing(rect)
                        }
                        }@

                        Open a text editor and drag rectangle to that text editor. Width and Height of that rectangle will be displayed on text editor

                        The detail of drag and drop in C++ : http://qt-project.org/doc/qt-4.8/dnd.html

                        Anyway, thanks onek24 for your guide, hope to receive your help in future

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post