QPair Class
The QPair class is a template class that stores a pair of items. More...
Header: | #include <QPair> |
qmake: | QT += core |
Public Types
typedef | first_type |
typedef | second_type |
Public Variables
Related Non-Members
QPair<T1, T2> | qMakePair(const T1 &x, const T2 &y) |
bool | operator!=(const int &p1, const int &p2) |
bool | operator==(const int &p1, const int &p2) |
Detailed Description
QPair<T1, T2> can be used in your application if the STL pair
type is not available. It stores one value of type T1 and one value of type T2. It can be used as a return value for a function that needs to return two values, or as the value type of a generic container.
Here's an example of a QPair that stores one QString and one double
value:
QPair<QString, double> pair;
The components are accessible as public data members called first and second. For example:
pair.first = "pi"; pair.second = M_PI;
Note, however, that it is almost always preferable to define a small struct to hold the result of a function with multiple return values. A struct trivially generalizes to more than two values, and allows more descriptive member names than first
and second
:
struct Variable { QString name; double value; }; Variable v; v.name = "pi"; v.value = M_PI;
The advent of C++11 automatic variable type deduction (auto
) shifts the emphasis from the type name to the name of functions and members. Thus, QPair, like std::pair
and std::tuple
, is mostly useful in generic (template) code, where defining a dedicated type is not possible.
QPair's template data types (T1 and T2) must be assignable data types. You cannot, for example, store a QWidget as a value; instead, store a QWidget *. A few functions have additional requirements; these requirements are documented on a per-function basis.
See also Container Classes.
Member Type Documentation
typedef QPair::first_type
The type of the first element in the pair (T1).
See also first.
typedef QPair::second_type
The type of the second element in the pair (T2).
See also second.
Member Variable Documentation
T1 QPair::first
The first element in the pair.
T2 QPair::second
The second element in the pair.
Related Non-Members
QPair<T1, T2> QPair::qMakePair(const T1 &x, const T2 &y)
Returns a QPair<T1, T2> that contains value1 and value2. Example:
QList<QPair<int, double> > list; list.append(qMakePair(66, M_PI));
This is equivalent to QPair<T1, T2>(value1, value2), but usually requires less typing.
bool QPair::operator!=(const int &p1, const int &p2)
Returns true
if lhs and rhs are different; otherwise returns false
.
This function was introduced in Qt 5.0.
bool QPair::operator==(const int &p1, const int &p2)
Returns true
if lhs and rhs are equal; otherwise returns false
.
This function was introduced in Qt 5.0.