简体   繁体   English

QT C ++我可以使用Custom mime-type在多个应用程序上进行复制和粘贴吗?

[英]QT C++ Can I use Custom mime-type for copy and paste on multiple application?

Recently , I made some application that using custom mime-type for copy and paste. 最近,我做了一个使用自定义mime-type复制和粘贴的应用程序。

I use vs2015 with Qt 5.7.0 . 我在Qt 5.7.0中使用vs2015。

In single application, copy and paste works well, 在单个应用程序中,复制和粘贴效果很好,

but When I execute A.exe and A'.exe (same application built by same code) , 但是当我执行A.exe和A'.exe(由相同代码构建的同一应用程序)时,

custom mime-type and qclipboard do not work correctly between A.exe and A'.exe 自定义的MIME类型和qclipboard在A.exe和A'.exe之间无法正常工作

Otherwise each single application's function works well. 否则,每个应用程序的功能都可以正常运行。

When I copied data on A.exe and pasted it to A'.exe , custom Mimedata is NULL. 当我在A.exe上复制数据并将其粘贴到A'.exe时,自定义Mimedata为NULL。

Is there any method to solve this problem without using QbyteArray? 有没有不使用QbyteArray即可解决此问题的方法?

Belows are my function briefly. 以下是我的简要功能。

copy : 复制:

QClipboard* _clipboard = QApplication::clipboard();

mycustomMimedata* _Mimedata = new mycustomMimedata();

_clipboard->setMimeData(_Mimedata);

paste : 粘贴:

QClipboard* _clipboard = QApplication::clipboard();

const mycustomMimedata* _mimeData = 
qobject_cast<const mycustomMimedata*>(_clipboard->mimeData());

The memory you allocate is owned by the process who creates it. 您分配的内存由创建它的进程拥有。 Other processes cannot access it. 其他进程无法访问它。 And you have 2 different processes here. 您在这里有2个不同的过程。

When you allocate mycustomMimedata and store it in the mime-data you are actually storing a pointer in the mime data. 当分配mycustomMimedata并将其存储在mime数据中时,实际上是在mime数据中存储了一个指针。 This may has the address 5 (just a random number) in your application A.exe. 您的应用程序A.exe中的地址可能为5(只是一个随机数)。

Now the other application has his own memory and at address 5 nothing or maybe something else is. 现在,另一个应用程序拥有自己的内存,并且在地址5处什么也没有,也许还有其他东西。 So when you "paste" you say "get me something from memory adress 5" and Qt seems smart enough to know that this is not valid and gives you a null pointer. 因此,当您“粘贴”时,您说“从内存地址5给我一些东西”,Qt似乎很聪明,知道这是无效的,并且为您提供了空指针。


Possible solutions: 可能的解决方案:

If you only need a "copy" you can make the class serializable and set this data as MIME value and deserialize it on the paste operation. 如果只需要“复制”,则可以使该类可序列化,并将此数据设置为MIME值,然后在粘贴操作中反序列化。 Or if you load it from a database use the ID to reload the object in your other application. 或者,如果您从数据库中加载它,请使用该ID在其他应用程序中重新加载该对象。

Copying your data into a QByteArray can be dangerous if you have a non-POD type. 如果您具有非POD类型,则将数据复制到QByteArray可能很危险。 If it is POD it should be save. 如果是POD,则应保存。

If you need to manipulate the same instance in both applications you need to get into IPC and shared_memory. 如果需要在两个应用程序中操作同一实例,则需要进入IPC和shared_memory。 Luckily Qt has those also implemented. 幸运的是,Qt还实现了这些功能。 This will probably get too broad though for this answer but Qt has good documentation and examples: http://doc.qt.io/qt-5/qtcore-ipc-sharedmemory-example.html 尽管对于此答案,这可能会变得太宽泛,但是Qt具有良好的文档和示例: http : //doc.qt.io/qt-5/qtcore-ipc-sharedmemory-example.html

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

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