mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-04 16:25:27 +08:00
qt 6.5.1 original
This commit is contained in:
59
examples/corelib/tools/doc/src/contiguouscache.qdoc
Normal file
59
examples/corelib/tools/doc/src/contiguouscache.qdoc
Normal file
@ -0,0 +1,59 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\example tools/contiguouscache
|
||||
\title Contiguous Cache Example
|
||||
|
||||
\brief The Contiguous Cache example shows how to use QContiguousCache to manage memory usage for
|
||||
very large models. In some environments memory is limited and, even when it
|
||||
isn't, users still dislike an application using excessive memory.
|
||||
Using QContiguousCache to manage a list, rather than loading
|
||||
the entire list into memory, allows the application to limit the amount
|
||||
of memory it uses, regardless of the size of the data set it accesses.
|
||||
|
||||
The simplest way to use QContiguousCache is to cache as items are requested. When
|
||||
a view requests an item at row N it is also likely to ask for items at rows near
|
||||
to N.
|
||||
|
||||
\snippet tools/contiguouscache/randomlistmodel.cpp 0
|
||||
|
||||
After getting the row, the class determines if the row is in the bounds
|
||||
of the contiguous cache's current range. It would have been equally valid to
|
||||
simply have the following code instead.
|
||||
|
||||
\code
|
||||
while (row > m_rows.lastIndex())
|
||||
m_rows.append(fetchWord(m_rows.lastIndex()+1);
|
||||
while (row < m_rows.firstIndex())
|
||||
m_rows.prepend(fetchWord(m_rows.firstIndex()-1);
|
||||
\endcode
|
||||
|
||||
However a list will often jump rows if the scroll bar is used directly, resulting in
|
||||
the code above causing every row between the old and new rows to be fetched.
|
||||
|
||||
Using QContiguousCache::lastIndex() and QContiguousCache::firstIndex() allows
|
||||
the example to determine what part of the list the cache is currently caching.
|
||||
These values don't represent the indexes into the cache's own memory, but rather
|
||||
a virtual infinite array that the cache represents.
|
||||
|
||||
By using QContiguousCache::append() and QContiguousCache::prepend() the code ensures
|
||||
that items that may be still on the screen are not lost when the requested row
|
||||
has not moved far from the current cache range. QContiguousCache::insert() can
|
||||
potentially remove more than one item from the cache as QContiguousCache does not
|
||||
allow for gaps. If your cache needs to quickly jump back and forth between
|
||||
rows with significant gaps between them consider using QCache instead.
|
||||
|
||||
And that's it. A perfectly reasonable cache, using minimal memory for a very large
|
||||
list. In this case the accessor for getting the words into the cache
|
||||
generates random information rather than fixed information. This allows you
|
||||
to see how the cache range is kept for a local number of rows when running the
|
||||
example.
|
||||
|
||||
\snippet tools/contiguouscache/randomlistmodel.cpp 1
|
||||
|
||||
It is also worth considering pre-fetching items into the cache outside of the
|
||||
application's paint routine. This can be done either with a separate thread
|
||||
or using a QTimer to incrementally expand the range of the cache prior to
|
||||
rows being requested out of the current cache range.
|
||||
*/
|
111
examples/corelib/tools/doc/src/customtype.qdoc
Normal file
111
examples/corelib/tools/doc/src/customtype.qdoc
Normal file
@ -0,0 +1,111 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\example tools/customtype
|
||||
\title Custom Type Example
|
||||
|
||||
\brief The Custom Type example shows how to integrate a custom type into Qt's
|
||||
meta-object system.
|
||||
|
||||
Contents:
|
||||
|
||||
\tableofcontents
|
||||
|
||||
\section1 Overview
|
||||
|
||||
Qt provides a range of standard value types that are used to provide
|
||||
rich and meaningful APIs. These types are integrated with the meta-object
|
||||
system, enabling them to be stored in QVariant objects, written out in
|
||||
debugging information and sent between components in signal-slot
|
||||
communication.
|
||||
|
||||
Custom types can also be integrated with the meta-object system as long as
|
||||
they are written to conform to some simple guidelines. In this example, we
|
||||
introduce a simple \c Message class, we describe how we make it work with
|
||||
QVariant, and we show how it can be extended to generate a printable
|
||||
representation of itself for use in debugging output.
|
||||
|
||||
\section1 The Message Class Definition
|
||||
|
||||
The \c Message class is a simple value class that contains two pieces
|
||||
of information (a QString and a QStringList), each of which can be read
|
||||
using trivial getter functions:
|
||||
|
||||
\snippet tools/customtype/message.h custom type definition
|
||||
|
||||
The default constructor, copy constructor and destructor are
|
||||
all required, and must be public, if the type is to be integrated into the
|
||||
meta-object system. Other than this, we are free to implement whatever we
|
||||
need to make the type do what we want, so we also include a constructor
|
||||
that lets us set the type's data members.
|
||||
|
||||
To enable the type to be used with QVariant, we declare it using the
|
||||
Q_DECLARE_METATYPE() macro:
|
||||
|
||||
\snippet tools/customtype/message.h custom type meta-type declaration
|
||||
|
||||
We do not need to write any additional code to accompany this macro.
|
||||
|
||||
To allow us to see a readable description of each \c Message object when it
|
||||
is sent to the debug output stream, we define a streaming operator:
|
||||
|
||||
\snippet tools/customtype/message.h custom type streaming operator
|
||||
|
||||
This facility is useful if you need to insert tracing statements in your
|
||||
code for debugging purposes.
|
||||
|
||||
\section1 The Message Class Implementation
|
||||
|
||||
The streaming operator is implemented in the following way:
|
||||
|
||||
\snippet tools/customtype/message.cpp custom type streaming operator
|
||||
|
||||
Here, we want to represent each value depending on how many lines are stored
|
||||
in the message body. We stream text to the QDebug object passed to the
|
||||
operator and return the QDebug object obtained from its maybeSpace() member
|
||||
function; this is described in more detail in the
|
||||
\l{Creating Custom Qt Types#Making the Type Printable}{Creating Custom Qt Types}
|
||||
document.
|
||||
|
||||
We include the code for the getter functions for completeness:
|
||||
|
||||
\snippet tools/customtype/message.cpp getter functions
|
||||
|
||||
With the type fully defined, implemented, and integrated with the
|
||||
meta-object system, we can now use it.
|
||||
|
||||
\section1 Using the Message
|
||||
|
||||
In the example's \c{main()} function, we show how a \c Message object can
|
||||
be printed to the console by sending it to the debug stream:
|
||||
|
||||
\snippet tools/customtype/main.cpp printing a custom type
|
||||
|
||||
You can use the type with QVariant in exactly the same way as you would
|
||||
use standard Qt value types. Here's how to store a value using the
|
||||
QVariant::setValue() function:
|
||||
|
||||
\snippet tools/customtype/main.cpp storing a custom value
|
||||
|
||||
Alternatively, the QVariant::fromValue() function can be used if
|
||||
you are using a compiler without support for member template
|
||||
functions.
|
||||
|
||||
The value can be retrieved using the QVariant::value() member template
|
||||
function:
|
||||
|
||||
\snippet tools/customtype/main.cpp retrieving a custom value
|
||||
|
||||
\section1 Further Reading
|
||||
|
||||
The custom \c Message type can also be used with direct signal-slot
|
||||
connections.
|
||||
|
||||
To register a custom type for use with queued signals and slots, such as
|
||||
those used in cross-thread communication, see the
|
||||
\l{Queued Custom Type Example}.
|
||||
|
||||
More information on using custom types with Qt can be found in the
|
||||
\l{Creating Custom Qt Types} document.
|
||||
*/
|
Reference in New Issue
Block a user