找回密码
 立即注册
收起左侧

我想问下按着《快速入门》的代码,一直连不上共享内存

2
回复
6176
查看
[复制链接]
累计签到:33 天
连续签到:1 天
来源: 2017-9-18 10:38:35 显示全部楼层 |阅读模式
10Qter豆
如题,愿被以为自己的问题,但是跑了一下示例代码也是不行,求指教。代码书上p450-p452
贴一下代码:
///.h
#ifndef DIALOG_H
#define DIALOG_H


#include <QDialog>
#include <QSharedMemory>


namespace Ui {
class Dialog;
}


class Dialog : public QDialog
{
    Q_OBJECT


public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();


private:
    Ui::Dialog *ui;
public slots:
    void loadFromFile();
    void loadFromMemory();
private slots:
    void on_loadFromFileButton_clicked();


    void on_loadFromSharedMemoryButon_clicked();


private:
    void detach();
    QSharedMemory sharedMemory;
};


#endif // DIALOG_H

///.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
#include <QBuffer>
#include <QDebug>


Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    sharedMemory.setKey("QSharedMemoryExample");
    ui->setupUi(this);


}


Dialog::~Dialog()
{
    delete ui;
}


void Dialog::loadFromFile()
{
    if(sharedMemory.isAttached())
        detach();
    ui->label->setText(tr("选择一个图片文件!"));
    QString fileName = QFileDialog::getOpenFileName(0,QString(),QString(),
                                                    tr("Images(*.png *.jpg)"));
    QImage image;
    if(!image.load(fileName)){
        ui->label->setText("选择的文件不是图片,请选择图片文件!");
        return;
    }
    ui->label->setPixmap(QPixmap::fromImage(image));
    //将图片加载到共享内存
    QBuffer buffer;
    buffer.open(QBuffer::ReadWrite);
    QDataStream out(&buffer);
    out << image;
    int size = buffer.size();
    if(!sharedMemory.create(size)){
        ui->label->setText(tr("无法创建共享内存段!"));
        return;
    }
    sharedMemory.lock();
    char * to = (char *) sharedMemory.data();
    const char * from = buffer.data().data();
    memcpy(to,from,qMin(sharedMemory.size(),size));
    sharedMemory.unlock();


}


void Dialog::loadFromMemory()
{
    if(!sharedMemory.attach()){
//        qDebug()<< sharedMemory.error();
        ui->label->setText(tr("无法连接到共享内存段,\n"
                              "请先加载一张图片"));
        return;
    }
    QBuffer buffer;
    QDataStream in(&buffer);
    QImage image;
    sharedMemory.lock();
    buffer.setData((char*)sharedMemory.constData(),sharedMemory.size());
    buffer.open(QBuffer::ReadOnly);
    in >> image;
    sharedMemory.unlock();
    sharedMemory.detach();
    ui->label->setPixmap(QPixmap::fromImage(image));
}


void Dialog::detach()
{
    if(!sharedMemory.detach())
        ui->label->setText(tr("无法从共享内存中分离"));


}


void Dialog::on_loadFromFileButton_clicked()
{
    loadFromFile();


}


void Dialog::on_loadFromSharedMemoryButon_clicked()
{
    loadFromMemory();
}

最佳答案

查看完整内容

共享内存得开两个进程,这才能谈共享,把你的debug目录下的exe运行两遍试试,我跑过书上的代码,没有问题
回复

使用道具 举报

累计签到:6 天
连续签到:1 天
2017-9-18 10:38:36 显示全部楼层
共享内存得开两个进程,这才能谈共享,把你的debug目录下的exe运行两遍试试,我跑过书上的代码,没有问题
回复

使用道具 举报

累计签到:33 天
连续签到:1 天
2017-9-25 13:10:47 显示全部楼层
biewenwoaaaa 发表于 2017-9-20 10:11
共享内存得开两个进程,这才能谈共享,把你的debug目录下的exe运行两遍试试,我跑过书上的代码,没有问题 ...

看书不认真,惭愧惭愧。明白了,谢谢。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了