简体   繁体   English

如何实现2个应用程序之间的拖放?

[英]How to implement drag&drop between 2 applications?

I'm trying to create an application, which will communicate through QLocalServer / QLocalSocket . 我正在尝试创建一个应用程序,它将通过QLocalServer / QLocalSocket通信。 The server name passing mechanism I planned to implement using the drag&drop mechanism. 我计划使用拖放机制实现服务器名称传递机制。 The mechanism is like the following: 该机制如下所示:

  • create QLocalServer in one application and QLocalSocket in the other, 创建QLocalServer在一个应用程序,并QLocalSocket在另一方面,
  • the server starts listening to some address, 服务器开始监听某个地址,
  • when you drag&drop some specified widget from the first app to the other, it must pass the address via QMimeData , 当您将某些指定的小部件从第一个应用程序拖放到另一个应用程序时,它必须通过QMimeData传递地址,
  • the second app gets the server address and connects to it. 第二个应用程序获取服务器地址并连接到它。

I have implemented some code, but it seems, that drag&drop between applications make some changes in mimeData object. 我已经实现了一些代码,但是看来,应用程序之间的拖放对mimeData对象进行了一些更改。 Here are code snippets and the result I have got: 以下是代码片段以及我得到的结果:

mouseMoveEvent(QMouseEvent* event)
{
    if (!__drag_options.__drag_started)
        return;

    if (distance(__drag_options.__drag_started_position, event->pos()) < DRAG_DISTANCE)
        return;

    QDrag drag(this);
    QMimeData* mimeData = new QMimeData;
    mimeData->setData("type", "pin");
    mimeData->setData("address", __address);
    drag.setMimeData(mimeData);
    drag.exec(Qt::MoveAction);
}

dropEvent(QDropEvent* event)
{
    qDebug() << "dropEvent " << event->mimeData()->formats();
    const QMimeData* mime = event->mimeData();
    QString serverName = mime->data("pin_name");
    __socket->connectToServer(serverName);
}

and the result is 结果是

dragEnterEvent ("application/x-qt-windows-mime;value=\\"type\\"", "application/x-qt-windows-mime;value=\\"address\\"") dragEnterEvent(“ application / x-qt-windows-mime; value = \\” type \\“”,“ application / x-qt-windows-mime; value = \\” address \\“”)

As you can see there's no mime named "address". 如您所见,没有一个名为“地址”的哑剧。

Any suggestions on how to get to the target? 关于如何达到目标的任何建议?

I suggest you use a standard mime type like json or application/json (and send your data encoded in JSON), or XML, or ... 我建议您使用标准的mime类型,例如jsonapplication/json (并发送以JSON编码的数据),XML或...

If you use a custom mime type, you have to live with the fact that Qt changes that mime type so it is more standard conformant. 如果使用自定义的mime类型,则必须忍受Qt更改该mime类型的事实,以便它更符合标准。

As long as you stay within one application, your mime types won't be touched. 只要您停留在一个应用程序中,您的mime类型就不会受到影响。

As soon as you drag from one application to another, you need the clipboard or such of the platform. 从一个应用程序拖到另一个应用程序后,就需要平台的剪贴板等。 (If I recall correctly, Windows only supports some of the possible mime types.) (如果我没记错的话,Windows仅支持某些可能的mime类型。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM