Congratulations to our 2022 Qt Champions!

Drag an Item to another application (Solved)

  • 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!

  • Hello,

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

  • 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?

  • 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.

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

    Thank you!

  • 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.

  • 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
                    print("dragsource =",drag.source)
    Text {
        id: txtDrag
        text: qsTr("Hello World")
        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.onDragFinished: {
            print("Finish Dragging");
          MouseArea {
              anchors.fill: parent


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

  • 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 :

  • 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_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
    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++ :

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

Log in to reply