|
本帖最后由 侠隐 于 2015-3-2 17:27 编辑
- //.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;
- QSharedMemory shareMemory;
- void detach();
- private slots:
- void loadFromFile();
- void loadFromMemory();
- void on_pushButton_clicked();
- void on_pushButton_2_clicked();
- };
- #endif // DIALOG_H
复制代码 这里有一个问题不是很理解,我测试时,开启了一个窗口,也是一个进程,这里称线程1,然后从文件加载了一张图片,这个时候再从共享内存中显示图片,结果就是下面的无法连接的结果,如下个代码
然后再开一个新的进程,这里称线程2,这个时候可以从共享内存中显示图片- //无法连接的代码段
- if(shareMemory.isattached()) qDebug()<<"haha";
- if(!shareMemory.attach()){
- ui->label->setText(tr("无法连接到共享内存段,\n"
- "请先加载一张图片!"));
- qDebug()<< shareMemory.errorString();
- return;
- }
复制代码 从上图的代码看,我使用了QShareMemory::isAttached()测试得知此时线程1是有与共享内存连接的,
但是接下去的
f(!shareMemory.attach())
却返回了true,也就是连接失败,这是为什么呢?
@yafeilinux
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|