• 大小: 734KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: 其他
  • 标签: qt  共享内存  

资源简介

qt写的共享内存简单例子,希望对你起到抛砖引玉的作用

资源截图

代码片段和文件信息

#include “dialog.h“
#include “ui_dialog.h“
#include 
#include 

Dialog::Dialog(QWidget *parent) :
    QDialog(parent)
    ui(new Ui::Dialog)sharedMem(“QSharedMemoryDemo“)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
if (sharedMem.isAttached())
detach();
}

void Dialog::on_WritePushButton_clicked()
{
 if (sharedMem.isAttached())
        detach();

QString strTmp = ui->WriteLineEdit->text();

QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);
out << strTmp;
int size = buffer.size();

//    int size = strTmp.size();

if (strTmp.size() <= 0)
{
QMessageBox::information(this“tip““please enter first!“);
return;
}
    if (!sharedMem.create(size QSharedMemory::ReadWrite)) {
        QMessageBox::critical(this“warning““Unable to create shared memory segment!“);
        return;
    }
    sharedMem.lock();
    char *to = (char*)sharedMem.data();
    const char *from = buffer.data().data();
    memcpy(to from qMin(sharedMem.size() size));
    sharedMem.unlock();
}

void Dialog::on_ReadPushButton_clicked()
{
if (!sharedMem.isAttached())
{
if (!sharedMem.attach()) {
QMessageBox::critical(this“warning““Unable to attach to shared memory segment!“);
return;
}
}

QBuffer buffer;
QDataStream in(&buffer);
QString strTmp;

sharedMem.lock();
buffer.setData((char*)sharedMem.constData() sharedMem.size());
buffer.open(QBuffer::ReadOnly);
in >> strTmp;
sharedMem.unlock();

ui->ReadLineEdit->setText(strTmp);
}

void Dialog::detach()
{
if (!sharedMem.detach()) 
QMessageBox::critical(this“警告““Unable to detach from shared memory“);
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-03-21 21:51  SharedMemoryDemo\
     目录           0  2012-12-26 15:25  SharedMemoryDemo\GeneratedFiles\
     目录           0  2013-03-21 21:51  SharedMemoryDemo\GeneratedFiles\Debug\
     目录           0  2013-03-21 21:51  SharedMemoryDemo\GeneratedFiles\Release\
     文件        3520  2012-12-26 15:55  SharedMemoryDemo\GeneratedFiles\ui_dialog.h
     文件         377  2012-12-26 14:04  SharedMemoryDemo\SharedMemoryDemo.pro
     文件       12827  2012-12-26 16:24  SharedMemoryDemo\SharedMemoryDemo.pro.user
     文件        1851  2012-12-27 15:56  SharedMemoryDemo\SharedMemoryDemo.sln
     文件       22016  2013-01-11 16:23  SharedMemoryDemo\SharedMemoryDemo.suo
     文件       13463  2013-01-11 16:22  SharedMemoryDemo\SharedMemoryDemo.vcxproj
     文件        3133  2013-01-11 16:22  SharedMemoryDemo\SharedMemoryDemo.vcxproj.filters
     文件         577  2013-01-11 16:22  SharedMemoryDemo\SharedMemoryDemo.vcxproj.user
     目录           0  2012-12-26 15:25  SharedMemoryDemo\Win32\
     目录           0  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\
     文件       70600  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\CL.read.1.tlog
     文件        1472  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\CL.write.1.tlog
     文件         638  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\SharedMemoryDemo.exe.intermediate.manifest
     文件          61  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\SharedMemoryDemo.lastbuildstate
     文件        3087  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\SharedMemoryDemo.log
     文件        4090  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\cl.command.1.tlog
     文件        1362  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\custombuild.command.1.tlog
     文件         268  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\custombuild.read.1.tlog
     文件         418  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\custombuild.write.1.tlog
     文件      121736  2012-12-26 16:42  SharedMemoryDemo\Win32\Debug\dialog.obj
     文件        2022  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\link.command.1.tlog
     文件        2994  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\link.read.1.tlog
     文件         848  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\link.write.1.tlog
     文件       48013  2012-12-26 15:25  SharedMemoryDemo\Win32\Debug\main.obj
     文件       51291  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\moc_dialog.obj
     文件         440  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\mt.command.1.tlog
     文件         806  2013-01-11 12:58  SharedMemoryDemo\Win32\Debug\mt.read.1.tlog
............此处省略10个文件信息

评论

共有 条评论