1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "dialog.h"
|
---|
43 | #include <QFileDialog>
|
---|
44 | #include <QBuffer>
|
---|
45 | #include <QtCore/QDebug>
|
---|
46 |
|
---|
47 | /*!
|
---|
48 | \class Dialog
|
---|
49 |
|
---|
50 | \brief This class is a simple example of how to use QSharedMemory.
|
---|
51 |
|
---|
52 | It is a simple dialog that presents a few buttons. To compile the
|
---|
53 | example, run make in qt/examples/ipc. Then run the executable twice
|
---|
54 | to create two processes running the dialog. In one of the processes,
|
---|
55 | press the button to load an image into a shared memory segment, and
|
---|
56 | then select an image file to load. Once the first process has loaded
|
---|
57 | and displayed the image, in the second process, press the button to
|
---|
58 | read the same image from shared memory. The second process displays
|
---|
59 | the same image loaded from its new loaction in shared memory.
|
---|
60 | */
|
---|
61 |
|
---|
62 | /*!
|
---|
63 | The class contains a data member \l {QSharedMemory} {sharedMemory},
|
---|
64 | which is initialized with the key "QSharedMemoryExample" to force
|
---|
65 | all instances of Dialog to access the same shared memory segment.
|
---|
66 | The constructor also connects the clicked() signal from each of the
|
---|
67 | three dialog buttons to the slot function appropriate for handling
|
---|
68 | each button.
|
---|
69 | */
|
---|
70 | //! [0]
|
---|
71 | Dialog::Dialog(QWidget *parent)
|
---|
72 | : QDialog(parent), sharedMemory("QSharedMemoryExample")
|
---|
73 | {
|
---|
74 | ui.setupUi(this);
|
---|
75 | connect(ui.loadFromFileButton, SIGNAL(clicked()), SLOT(loadFromFile()));
|
---|
76 | connect(ui.loadFromSharedMemoryButton,
|
---|
77 | SIGNAL(clicked()),
|
---|
78 | SLOT(loadFromMemory()));
|
---|
79 | setWindowTitle(tr("SharedMemory Example"));
|
---|
80 | }
|
---|
81 | //! [0]
|
---|
82 |
|
---|
83 | /*!
|
---|
84 | This slot function is called when the \tt {Load Image From File...}
|
---|
85 | button is pressed on the firs Dialog process. First, it tests
|
---|
86 | whether the process is already connected to a shared memory segment
|
---|
87 | and, if so, detaches from that segment. This ensures that we always
|
---|
88 | start the example from the beginning if we run it multiple times
|
---|
89 | with the same two Dialog processes. After detaching from an existing
|
---|
90 | shared memory segment, the user is prompted to select an image file.
|
---|
91 | The selected file is loaded into a QImage. The QImage is displayed
|
---|
92 | in the Dialog and streamed into a QBuffer with a QDataStream.
|
---|
93 |
|
---|
94 | Next, it gets a new shared memory segment from the system big enough
|
---|
95 | to hold the image data in the QBuffer, and it locks the segment to
|
---|
96 | prevent the second Dialog process from accessing it. Then it copies
|
---|
97 | the image from the QBuffer into the shared memory segment. Finally,
|
---|
98 | it unlocks the shared memory segment so the second Dialog process
|
---|
99 | can access it.
|
---|
100 |
|
---|
101 | After this function runs, the user is expected to press the \tt
|
---|
102 | {Load Image from Shared Memory} button on the second Dialog process.
|
---|
103 |
|
---|
104 | \sa loadFromMemory()
|
---|
105 | */
|
---|
106 | //! [1]
|
---|
107 | void Dialog::loadFromFile()
|
---|
108 | {
|
---|
109 | if (sharedMemory.isAttached())
|
---|
110 | detach();
|
---|
111 |
|
---|
112 | ui.label->setText(tr("Select an image file"));
|
---|
113 | QString fileName = QFileDialog::getOpenFileName(0, QString(), QString(),
|
---|
114 | tr("Images (*.png *.xpm *.jpg)"));
|
---|
115 | QImage image;
|
---|
116 | if (!image.load(fileName)) {
|
---|
117 | ui.label->setText(tr("Selected file is not an image, please select another."));
|
---|
118 | return;
|
---|
119 | }
|
---|
120 | ui.label->setPixmap(QPixmap::fromImage(image));
|
---|
121 | //! [1] //! [2]
|
---|
122 |
|
---|
123 | // load into shared memory
|
---|
124 | QBuffer buffer;
|
---|
125 | buffer.open(QBuffer::ReadWrite);
|
---|
126 | QDataStream out(&buffer);
|
---|
127 | out << image;
|
---|
128 | int size = buffer.size();
|
---|
129 |
|
---|
130 | if (!sharedMemory.create(size)) {
|
---|
131 | ui.label->setText(tr("Unable to create shared memory segment."));
|
---|
132 | return;
|
---|
133 | }
|
---|
134 | sharedMemory.lock();
|
---|
135 | char *to = (char*)sharedMemory.data();
|
---|
136 | const char *from = buffer.data().data();
|
---|
137 | memcpy(to, from, qMin(sharedMemory.size(), size));
|
---|
138 | sharedMemory.unlock();
|
---|
139 | }
|
---|
140 | //! [2]
|
---|
141 |
|
---|
142 | /*!
|
---|
|
---|