source: trunk/doc/src/snippets/buffer/buffer.cpp@ 244

Last change on this file since 244 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 3.5 KB
Line 
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 documentation 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 <QApplication>
43#include <QBuffer>
44#include <QPalette>
45
46static void main_snippet()
47{
48//! [0]
49 QBuffer buffer;
50 char ch;
51
52 buffer.open(QBuffer::ReadWrite);
53 buffer.write("Qt rocks!");
54 buffer.seek(0);
55 buffer.getChar(&ch); // ch == 'Q'
56 buffer.getChar(&ch); // ch == 't'
57 buffer.getChar(&ch); // ch == ' '
58 buffer.getChar(&ch); // ch == 'r'
59//! [0]
60}
61
62static void write_datastream_snippets()
63{
64//! [1]
65 QByteArray byteArray;
66 QBuffer buffer(&byteArray);
67 buffer.open(QIODevice::WriteOnly);
68
69 QDataStream out(&buffer);
70 out << QApplication::palette();
71//! [1]
72}
73
74static void read_datastream_snippets()
75{
76 QByteArray byteArray;
77
78//! [2]
79 QPalette palette;
80 QBuffer buffer(&byteArray);
81 buffer.open(QIODevice::ReadOnly);
82
83 QDataStream in(&buffer);
84 in >> palette;
85//! [2]
86}
87
88static void bytearray_ptr_ctor_snippet()
89{
90//! [3]
91 QByteArray byteArray("abc");
92 QBuffer buffer(&byteArray);
93 buffer.open(QIODevice::WriteOnly);
94 buffer.seek(3);
95 buffer.write("def", 3);
96 buffer.close();
97 // byteArray == "abcdef"
98//! [3]
99}
100
101static void setBuffer_snippet()
102{
103//! [4]
104 QByteArray byteArray("abc");
105 QBuffer buffer;
106 buffer.setBuffer(&byteArray);
107 buffer.open(QIODevice::WriteOnly);
108 buffer.seek(3);
109 buffer.write("def", 3);
110 buffer.close();
111 // byteArray == "abcdef"
112//! [4]
113}
114
115int main(int argc, char *argv[])
116{
117 QApplication app(argc, argv);
118
119 main_snippet();
120 bytearray_ptr_ctor_snippet();
121 write_datastream_snippets();
122 read_datastream_snippets();
123 setBuffer_snippet();
124 return 0;
125}
Note: See TracBrowser for help on using the repository browser.