简体   繁体   中英

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.

I use vs2015 with Qt 5.7.0 .

In single application, copy and paste works well,

but When I execute A.exe and A'.exe (same application built by same code) ,

custom mime-type and qclipboard do not work correctly between A.exe and 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.

Is there any method to solve this problem without using 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.

When you allocate mycustomMimedata and store it in the mime-data you are actually storing a pointer in the mime data. This may has the address 5 (just a random number) in your application A.exe.

Now the other application has his own memory and at address 5 nothing or maybe something else is. 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.


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. Or if you load it from a database use the ID to reload the object in your other application.

Copying your data into a QByteArray can be dangerous if you have a non-POD type. If it is POD it should be save.

If you need to manipulate the same instance in both applications you need to get into IPC and shared_memory. Luckily Qt has those also implemented. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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