QT C++ 我可以使用自定义 mime 类型在多个应用程序上进行复制和粘贴吗?
在我问了这个问题之后,我尝试使用 QSharedMemory 制作一个应用程序。
但是当我尝试在 A 和 A' 之间复制和粘贴时
发生无法读取内存。(在单个应用程序中,它完美地工作)
下面是我的结果和代码。
我的自定义课程
class CustomVector{
public: vector<CustomData*> vecs;
};
复制功能
sharedMemory.setKey("TestKey"); // setKey to identify
if (sharedMemory.isAttached()) {
if (!sharedMemory.detach()) {
QMessageBox msg;
msg.setText(tr("Unable to detach from shared memory."));
msg.exec();
}
}
CustomVector* from = new CustomVector(); //
// do some work for adding CustomData* data to from using pushback (omit)
int size = sizeof(*from);
if (!sharedMemory.create(size)) { // create sharedmemory by size of from
cout << "Unable to create shared memory segment: " << sharedMemory.errorString().toStdString() << endl;
sharedMemory.detach();
sharedMemory.create(size);
cout << "detached" << endl;
}
sharedMemory.lock();
CustomVector* to = (CustomVector*)sharedMemory.data();
memcpy(to, from, sizeof(*from));
sharedMemory.unlock();
粘贴功能
sharedMemory.setKey("TestKey");
sharedMemory.attach();
CustomVector* to = new CustomVector();
sharedMemory.lock();
CustomVector* from = (CustomVector*)sharedMemory.data();
memcpy(to,from, sizeof(*from));
sharedMemory.unlock();
sharedMemory.detach();
// and do some work using pasted CustomVector* to (omit)
如何使用 QSharedMemory 和包含 vector 的自定义类在多个应用程序上复制和粘贴?