Qt主要是通过QSharedMemory类来访问以及操作共享内存。
以下是几个主要的步骤
利用QSharedMemory类创建实例对象时,必须为该共享内存指定关键字(即为该共享内存起一个名字)。只有当共享内存被设置了关键字之后,才可以执行创建create()、关联attach()等操作。为共享内存指定关键字有两种方法:
比如:
QSharedMemory sendsharedMem;
QSharedMemory recvsharedMem;
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),sendsharedMem("SHAREFILE"),recvsharedMem("SHAREFILE")
直接这样设置key
比如:
sendsharedMem.setKey("SHAREFILE");
recvsharedMem.setKey("SHAREFILE");
这样来设置
QSharedMemory::ReadOnly //只读方式访问共享内存
QSharedMemory::ReadWrite //读写方式访问共享内存
总的步骤大概是这些,下面用一个示例来说明
界面是这样,open用来打开本地图片并在左边label显示,save用来存储进共享空间,show用来读取共享空间的图在右边label显示,exit是退出
//首先是打开本地图片并显示
void Widget::InsertImage()
{
ui->btn_1 = qobject_cast(sender());
QString fileName = QFileDialog::getOpenFileName( this,
tr("打开文件"),
"C:/Users/Administrator/Desktop",
tr("任何文件(*.*)"
";;文本文件(*.txt)"
";;XML文件(*.xml)"
";;Images (*.png *.xpm *.jpg)"));
QMovie *move=new QMovie(fileName);
ui->lbl_1->setMovie(move);
move->start();
}
//然后是存储共享空间
void Widget::loadfromsharedMem()
{
sharedMemory.setKey("QSharedMemoryExample");// 设置访问标识
if (sharedMemory.isAttached())//判断是否连接到共享块
{
// 将该进程与共享内存段分离
if (!sharedMemory.detach())
qDebug() << "Unable to detach from shared memory.";
}
QString fileName = QFileDialog::getOpenFileName(0, tr("打开文件"),
"C:/Users/Administrator/Desktop",
tr("Images (*.png *.xpm *.jpg)"));
QImage image;
if (!image.load(fileName))
{
qDebug() << "Selected file is not an image, please select another.";
return;
}
// 将数据加载到共享内存中
QBuffer buffer; //将字符串转换为字节的介质
buffer.open(QBuffer::ReadWrite); //设置读取模式
QDataStream out(&buffer); //数据流
out << image;
int size = buffer.size(); //获得字节大小
// 创建共享内存段
if (!sharedMemory.create(size)) //检测共享内存是否创建成功
{
qDebug() << sharedMemory.errorString() << "\n Unable to create shared memory segment.";
return;
}
sharedMemory.lock(); //锁定共享内存
char *to = (char*)sharedMemory.data(); //共享内存的数据
const char *from = buffer.data().data(); //被共享内存的数据
memcpy(to, from, qMin(sharedMemory.size(), size)); //将要共享的内存copy到被共享的内存
sharedMemory.unlock();
}
//接下来是读取
void Widget::loadFromMemory()
{
receiveMemory.setKey("QSharedMemoryExample");//声明共享内存的密钥
// 将共享内存与该进程绑定
if (!receiveMemory.attach())
{
ui->lbl_2->setText("Unable to attach from shared memory.");
return;
}
// 从共享内存中读取数据
QBuffer buffer; //声明缓冲区
QDataStream in(&buffer) //声明数据流
QImage image;
receiveMemory.lock(); //锁定共享内存
//用缓冲区得到共享内存关联后得到的数据和数据大小
buffer.setData((char*)receiveMemory.constData(), receiveMemory.size())
buffer.open(QBuffer::ReadOnly); //设置读取模式
in >> image; //使用数据流从缓冲区获得共享内存的数据,然后输出到字符串中
receiveMemory.unlock(); //解锁
receiveMemory.detach();//与共享内存空间分离
ui->lbl_2->setPixmap(QPixmap::fromImage(image));
}
//最后是头文件内容
#include
#include
#include
#include
#include
#include
#include
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void loadfromsharedMem();
void loadFromMemory();
public slots:
void InsertImage();
private slots:
void on_btn_1_clicked();
void on_btn_2_clicked();
void on_btn_3_clicked();
void on_btn_4_clicked();
private:
Ui::Widget *ui;
QSharedMemory sharedMemory;
QSharedMemory receiveMemory;
};
如果是lineEdit输入文字也是很简单的
void Widget::sendtosharedMem()
{
send2sharedMem.setKey("QSharedMemoryExample");
if (send2sharedMem.isAttached())
{
// 将该进程与共享内存段分离
if (!send2sharedMem.detach())
qDebug() << "Unable to detach from shared memory.";
}
bool ok;
QString text = ui->textEdit->toPlainText();
ok = TRUE;
if (ok && text.length())
{
// 将数据加载到共享内存中
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);
out << text;
int size = buffer.size();
// 创建共享内存段
if (!send2sharedMem.create(size))
{
qDebug() << send2sharedMem.errorString() << "\n Unable to create shared memory segment.";
return;
}
send2sharedMem.lock();
char *to = (char*)send2sharedMem.data();
const char *from = buffer.data().data();
memcpy(to, from, qMin(send2sharedMem.size(), size));
send2sharedMem.unlock();
}
else
{
qDebug() << "You must write some texts.";
}
}
这是读取
void MainWidget::recvDate()
{
recvsharedMem.setKey("QSharedMemoryExample");
if (!recvsharedMem.attach())
{
ui->textEdit_2->setText("No Share Data");
return;
}
QBuffer buffer;
QDataStream in(&buffer);
QString text;
recvsharedMem.lock();
buffer.setData((char*)recvsharedMem.constData(), recvsharedMem.size());
buffer.open(QBuffer::ReadOnly);
in >> text;
recvsharedMem.unlock();
recvsharedMem.detach();
ui->textEdit_2->setText(text);
}