Qt 4.8
Public Functions | Static Public Functions | List of all members
QItemSelection Class Reference

The QItemSelection class manages information about selected items in a model. More...

#include <qitemselectionmodel.h>

Inheritance diagram for QItemSelection:
QList< QItemSelectionRange >

Public Functions

bool contains (const QModelIndex &index) const
 Returns true if the selection contains the given index; otherwise returns false. More...
 
QModelIndexList indexes () const
 Returns a list of model indexes that correspond to the selected items. More...
 
void merge (const QItemSelection &other, QItemSelectionModel::SelectionFlags command)
 Merges the other selection with this QItemSelection using the command given. More...
 
 QItemSelection ()
 Constructs an empty selection. More...
 
 QItemSelection (const QModelIndex &topLeft, const QModelIndex &bottomRight)
 Constructs an item selection that extends from the top-left model item, specified by the topLeft index, to the bottom-right item, specified by bottomRight. More...
 
void select (const QModelIndex &topLeft, const QModelIndex &bottomRight)
 Adds the items in the range that extends from the top-left model item, specified by the topLeft index, to the bottom-right item, specified by bottomRight to the list. More...
 
- Public Functions inherited from QList< QItemSelectionRange >
void append (const QItemSelectionRange &t)
 Inserts value at the end of the list. More...
 
void append (const QList< QItemSelectionRange > &t)
 Appends the items of the value list to this list. More...
 
const QItemSelectionRangeat (int i) const
 Returns the item at index position i in the list. More...
 
QItemSelectionRangeback ()
 This function is provided for STL compatibility. More...
 
const QItemSelectionRangeback () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
iterator begin ()
 Returns an STL-style iterator pointing to the first item in the list. More...
 
const_iterator begin () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void clear ()
 Removes all items from the list. More...
 
const_iterator constBegin () const
 Returns a const STL-style iterator pointing to the first item in the list. More...
 
const_iterator constEnd () const
 Returns a const STL-style iterator pointing to the imaginary item after the last item in the list. More...
 
QBool contains (const QItemSelectionRange &t) const
 Returns true if the list contains an occurrence of value; otherwise returns false. More...
 
int count (const QItemSelectionRange &t) const
 Returns the number of occurrences of value in the list. More...
 
int count () const
 Returns the number of items in the list. More...
 
void detach ()
 
void detachShared ()
 This prevents needless mallocs, and makes QList more exception safe in case of cleanup work done in destructors on empty lists. More...
 
bool empty () const
 This function is provided for STL compatibility. More...
 
iterator end ()
 Returns an STL-style iterator pointing to the imaginary item after the last item in the list. More...
 
const_iterator end () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool endsWith (const QItemSelectionRange &t) const
 Returns true if this list is not empty and its last item is equal to value; otherwise returns false. More...
 
iterator erase (iterator pos)
 Removes the item associated with the iterator pos from the list, and returns an iterator to the next item in the list (which may be end()). More...
 
iterator erase (iterator first, iterator last)
 Removes all the items from begin up to (but not including) end. More...
 
QItemSelectionRangefirst ()
 Returns a reference to the first item in the list. More...
 
const QItemSelectionRangefirst () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
QItemSelectionRangefront ()
 This function is provided for STL compatibility. More...
 
const QItemSelectionRangefront () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
int indexOf (const QItemSelectionRange &t, int from=0) const
 Returns the index position of the first occurrence of value in the list, searching forward from index position from. More...
 
void insert (int i, const QItemSelectionRange &t)
 Inserts value at index position i in the list. More...
 
iterator insert (iterator before, const QItemSelectionRange &t)
 Inserts value in front of the item pointed to by the iterator before. More...
 
bool isDetached () const
 
bool isEmpty () const
 Returns true if the list contains no items; otherwise returns false. More...
 
bool isSharedWith (const QList< QItemSelectionRange > &other) const
 
QItemSelectionRangelast ()
 Returns a reference to the last item in the list. More...
 
const QItemSelectionRangelast () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
int lastIndexOf (const QItemSelectionRange &t, int from=-1) const
 Returns the index position of the last occurrence of value in the list, searching backward from index position from. More...
 
int length () const
 This function is identical to count(). More...
 
QList< QItemSelectionRangemid (int pos, int length=-1) const
 Returns a list whose elements are copied from this list, starting at position pos. More...
 
void move (int from, int to)
 Moves the item at index position from to index position to. More...
 
bool operator!= (const QList< QItemSelectionRange > &l) const
 Returns true if other is not equal to this list; otherwise returns false. More...
 
QList< QItemSelectionRangeoperator+ (const QList< QItemSelectionRange > &l) const
 Returns a list that contains all the items in this list followed by all the items in the other list. More...
 
QList< QItemSelectionRange > & operator+= (const QList< QItemSelectionRange > &l)
 Appends the items of the other list to this list and returns a reference to this list. More...
 
QList< QItemSelectionRange > & operator+= (const QItemSelectionRange &t)
 Appends value to the list. More...
 
QList< QItemSelectionRange > & operator<< (const QList< QItemSelectionRange > &l)
 Appends the items of the other list to this list and returns a reference to this list. More...
 
QList< QItemSelectionRange > & operator<< (const QItemSelectionRange &t)
 Appends value to the list. More...
 
QList< QItemSelectionRange > & operator= (const QList< QItemSelectionRange > &l)
 Assigns other to this list and returns a reference to this list. More...
 
bool operator== (const QList< QItemSelectionRange > &l) const
 Returns true if other is equal to this list; otherwise returns false. More...
 
const QItemSelectionRangeoperator[] (int i) const
 Same as at(). More...
 
QItemSelectionRangeoperator[] (int i)
 Returns the item at index position i as a modifiable reference. More...
 
void pop_back ()
 This function is provided for STL compatibility. More...
 
void pop_front ()
 This function is provided for STL compatibility. More...
 
void prepend (const QItemSelectionRange &t)
 Inserts value at the beginning of the list. More...
 
void push_back (const QItemSelectionRange &t)
 This function is provided for STL compatibility. More...
 
void push_front (const QItemSelectionRange &t)
 This function is provided for STL compatibility. More...
 
 QList ()
 Constructs an empty list. More...
 
 QList (const QList< QItemSelectionRange > &l)
 Constructs a copy of other. More...
 
int removeAll (const QItemSelectionRange &t)
 Removes all occurrences of value in the list and returns the number of entries removed. More...
 
void removeAt (int i)
 Removes the item at index position i. More...
 
void removeFirst ()
 Removes the first item in the list. More...
 
void removeLast ()
 Removes the last item in the list. More...
 
bool removeOne (const QItemSelectionRange &t)
 Removes the first occurrence of value in the list and returns true on success; otherwise returns false. More...
 
void replace (int i, const QItemSelectionRange &t)
 Replaces the item at index position i with value. More...
 
void reserve (int size)
 Reserve space for alloc elements. More...
 
void setSharable (bool sharable)
 
int size () const
 Returns the number of items in the list. More...
 
bool startsWith (const QItemSelectionRange &t) const
 Returns true if this list is not empty and its first item is equal to value; otherwise returns false. More...
 
void swap (QList< QItemSelectionRange > &other)
 Swaps list other with this list. More...
 
void swap (int i, int j)
 Exchange the item at index position i with the item at index position j. More...
 
QItemSelectionRange takeAt (int i)
 Removes the item at index position i and returns it. More...
 
QItemSelectionRange takeFirst ()
 Removes the first item in the list and returns it. More...
 
QItemSelectionRange takeLast ()
 Removes the last item in the list and returns it. More...
 
QSet< QItemSelectionRangetoSet () const
 Returns a QSet object with the data contained in this QList. More...
 
std::list< QItemSelectionRangetoStdList () const
 Returns a std::list object with the data contained in this QList. More...
 
QVector< QItemSelectionRangetoVector () const
 Returns a QVector object with the data contained in this QList. More...
 
QItemSelectionRange value (int i) const
 Returns the value at index position i in the list. More...
 
QItemSelectionRange value (int i, const QItemSelectionRange &defaultValue) const
 If the index i is out of bounds, the function returns defaultValue. More...
 
 ~QList ()
 Destroys the list. More...
 

Static Public Functions

static void split (const QItemSelectionRange &range, const QItemSelectionRange &other, QItemSelection *result)
 Splits the selection range using the selection other range. More...
 
- Static Public Functions inherited from QList< QItemSelectionRange >
static QList< QItemSelectionRangefromSet (const QSet< QItemSelectionRange > &set)
 Returns a QList object with the data contained in set. More...
 
static QList< QItemSelectionRangefromStdList (const std::list< QItemSelectionRange > &list)
 Returns a QList object with the data contained in list. More...
 
static QList< QItemSelectionRangefromVector (const QVector< QItemSelectionRange > &vector)
 Returns a QList object with the data contained in vector. More...
 

Additional Inherited Members

- Public Types inherited from QList< QItemSelectionRange >
typedef const value_typeconst_pointer
 Typedef for const T *. More...
 
typedef const value_typeconst_reference
 Typedef for const T &. More...
 
typedef const_iterator ConstIterator
 Qt-style synonym for QList::const_iterator. More...
 
typedef qptrdiff difference_type
 Typedef for ptrdiff_t. More...
 
typedef iterator Iterator
 Qt-style synonym for QList::iterator. More...
 
typedef value_typepointer
 Typedef for T *. More...
 
typedef value_typereference
 Typedef for T &. More...
 
typedef int size_type
 Typedef for int. More...
 
typedef QItemSelectionRange value_type
 Typedef for T. More...
 
- Public Variables inherited from QList< QItemSelectionRange >
QListData::Datad
 
QListData p
 

Detailed Description

The QItemSelection class manages information about selected items in a model.

A QItemSelection describes the items in a model that have been selected by the user. A QItemSelection is basically a list of selection ranges, see QItemSelectionRange. It provides functions for creating and manipulating selections, and selecting a range of items from a model.

The QItemSelection class is one of the Model/View Classes and is part of Qt's Model/View Programming{model/view framework}.

An item selection can be constructed and initialized to contain a range of items from an existing model. The following example constructs a selection that contains a range of items from the given model, beginning at the topLeft, and ending at the bottomRight.

QItemSelection *selection = new QItemSelection(topLeft, bottomRight);

An empty item selection can be constructed, and later populated as required. So, if the model is going to be unavailable when we construct the item selection, we can rewrite the above code in the following way:

QItemSelection *selection = new QItemSelection();
...
selection->select(topLeft, bottomRight);

QItemSelection saves memory, and avoids unnecessary work, by working with selection ranges rather than recording the model item index for each item in the selection. Generally, an instance of this class will contain a list of non-overlapping selection ranges.

Use merge() to merge one item selection into another without making overlapping ranges. Use split() to split one selection range into smaller ranges based on a another selection range.

See also
{Model/View Programming}, QItemSelectionModel

Definition at line 231 of file qitemselectionmodel.h.

Constructors and Destructors

◆ QItemSelection() [1/2]

QItemSelection::QItemSelection ( )
inline

Constructs an empty selection.

Definition at line 234 of file qitemselectionmodel.h.

234 {}

◆ QItemSelection() [2/2]

QItemSelection::QItemSelection ( const QModelIndex topLeft,
const QModelIndex bottomRight 
)

Constructs an item selection that extends from the top-left model item, specified by the topLeft index, to the bottom-right item, specified by bottomRight.

Definition at line 451 of file qitemselectionmodel.cpp.

452 {
453  select(topLeft, bottomRight);
454 }
void select(const QModelIndex &topLeft, const QModelIndex &bottomRight)
Adds the items in the range that extends from the top-left model item, specified by the topLeft index...

Functions

◆ contains()

bool QItemSelection::contains ( const QModelIndex index) const

Returns true if the selection contains the given index; otherwise returns false.

Definition at line 491 of file qitemselectionmodel.cpp.

492 {
493  if (index.flags() & Qt::ItemIsSelectable) {
495  for (; it != end(); ++it)
496  if ((*it).contains(index))
497  return true;
498  }
499  return false;
500 }
#define it(className, varName)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
Qt::ItemFlags flags() const
Returns the flags for the item referred to by the index.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ indexes()

QModelIndexList QItemSelection::indexes ( ) const

Returns a list of model indexes that correspond to the selected items.

Definition at line 506 of file qitemselectionmodel.cpp.

Referenced by QCompleterPrivate::_q_completionSelected(), QTreeWidgetPrivate::_q_selectionChanged(), QAbstractItemViewPrivate::clearOrRemove(), QAbstractProxyModel::mapSelectionFromSource(), QAbstractProxyModel::mapSelectionToSource(), QItemSelectionModel::selectedIndexes(), QTableView::selectionChanged(), QListView::selectionChanged(), and QTreeView::selectionChanged().

507 {
508  QModelIndexList result;
510  for (; it != end(); ++it)
511  indexesFromRange(*it, result);
512  return result;
513 }
#define it(className, varName)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
static void indexesFromRange(const QItemSelectionRange &range, QModelIndexList &result)

◆ merge()

void QItemSelection::merge ( const QItemSelection other,
QItemSelectionModel::SelectionFlags  command 
)

Merges the other selection with this QItemSelection using the command given.

This method guarantees that no ranges are overlapping.

Note that only QItemSelectionModel::Select, QItemSelectionModel::Deselect, and QItemSelectionModel::Toggle are supported.

See also
split()

Definition at line 525 of file qitemselectionmodel.cpp.

Referenced by QItemSelectionModel::columnIntersectsSelection(), QItemSelectionModelPrivate::expandSelection(), QItemSelectionModelPrivate::finalize(), QItemSelectionModel::hasSelection(), QItemSelectionModel::rowIntersectsSelection(), QItemSelectionModel::select(), QItemSelectionModel::selectedIndexes(), QItemSelectionModel::selection(), and QListView::setSelection().

526 {
527  if (other.isEmpty() ||
528  !(command & QItemSelectionModel::Select ||
529  command & QItemSelectionModel::Deselect ||
530  command & QItemSelectionModel::Toggle))
531  return;
532 
533  QItemSelection newSelection = other;
534  // Collect intersections
535  QItemSelection intersections;
536  QItemSelection::iterator it = newSelection.begin();
537  while (it != newSelection.end()) {
538  if (!(*it).isValid()) {
539  it = newSelection.erase(it);
540  continue;
541  }
542  for (int t = 0; t < count(); ++t) {
543  if ((*it).intersects(at(t)))
544  intersections.append(at(t).intersected(*it));
545  }
546  ++it;
547  }
548 
549  // Split the old (and new) ranges using the intersections
550  for (int i = 0; i < intersections.count(); ++i) { // for each intersection
551  for (int t = 0; t < count();) { // splitt each old range
552  if (at(t).intersects(intersections.at(i))) {
553  split(at(t), intersections.at(i), this);
554  removeAt(t);
555  } else {
556  ++t;
557  }
558  }
559  // only split newSelection if Toggle is specified
560  for (int n = 0; (command & QItemSelectionModel::Toggle) && n < newSelection.count();) {
561  if (newSelection.at(n).intersects(intersections.at(i))) {
562  split(newSelection.at(n), intersections.at(i), &newSelection);
563  newSelection.removeAt(n);
564  } else {
565  ++n;
566  }
567  }
568  }
569  // do not add newSelection for Deselect
570  if (!(command & QItemSelectionModel::Deselect))
571  operator+=(newSelection);
572 }
#define it(className, varName)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
const QItemSelectionRange & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
friend class iterator
Definition: qlist.h:226
QList< QItemSelectionRange > & operator+=(const QList< QItemSelectionRange > &l)
Appends the items of the other list to this list and returns a reference to this list.
Definition: qlist.h:818
int count() const
Returns the number of items in the list.
Definition: qlist.h:280
iterator erase(iterator pos)
Removes the item associated with the iterator pos from the list, and returns an iterator to the next ...
Definition: qlist.h:464
The QItemSelection class manages information about selected items in a model.
bool intersects(const QItemSelectionRange &other) const
Returns true if this selection range intersects (overlaps with) the other range given; otherwise retu...
static void split(const QItemSelectionRange &range, const QItemSelectionRange &other, QItemSelection *result)
Splits the selection range using the selection other range.
void removeAt(int i)
Removes the item at index position i.
Definition: qlist.h:480

◆ select()

void QItemSelection::select ( const QModelIndex topLeft,
const QModelIndex bottomRight 
)

Adds the items in the range that extends from the top-left model item, specified by the topLeft index, to the bottom-right item, specified by bottomRight to the list.

Note
topLeft and bottomRight must have the same parent.

Definition at line 463 of file qitemselectionmodel.cpp.

Referenced by QListViewPrivate::selection(), and QListView::setSelection().

464 {
465  if (!topLeft.isValid() || !bottomRight.isValid())
466  return;
467 
468  if ((topLeft.model() != bottomRight.model())
469  || topLeft.parent() != bottomRight.parent()) {
470  qWarning("Can't select indexes from different model or with different parents");
471  return;
472  }
473  if (topLeft.row() > bottomRight.row() || topLeft.column() > bottomRight.column()) {
474  int top = qMin(topLeft.row(), bottomRight.row());
475  int bottom = qMax(topLeft.row(), bottomRight.row());
476  int left = qMin(topLeft.column(), bottomRight.column());
477  int right = qMax(topLeft.column(), bottomRight.column());
478  QModelIndex tl = topLeft.sibling(top, left);
479  QModelIndex br = bottomRight.sibling(bottom, right);
480  append(QItemSelectionRange(tl, br));
481  return;
482  }
483  append(QItemSelectionRange(topLeft, bottomRight));
484 }
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QModelIndex sibling(int row, int column) const
Returns the sibling at row and column.
The QItemSelectionRange class manages information about a range of selected items in a model...
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void append(const QItemSelectionRange &t)
Inserts value at the end of the list.
Definition: qlist.h:507
int row() const
Returns the row this model index refers to.
const QAbstractItemModel * model() const
Returns a pointer to the model containing the item that this index refers to.
Q_CORE_EXPORT void qWarning(const char *,...)
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QModelIndex class is used to locate data in a data model.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
int column() const
Returns the column this model index refers to.

◆ split()

void QItemSelection::split ( const QItemSelectionRange range,
const QItemSelectionRange other,
QItemSelection result 
)
static

Splits the selection range using the selection other range.

Removes all items in other from range and puts the result in result. This can be compared with the semantics of the subtract operation of a set.

See also
merge()

Definition at line 581 of file qitemselectionmodel.cpp.

Referenced by QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved(), and QItemSelectionModel::emitSelectionChanged().

583 {
584  if (range.parent() != other.parent() || range.model() != other.model())
585  return;
586 
587  QModelIndex parent = other.parent();
588  int top = range.top();
589  int left = range.left();
590  int bottom = range.bottom();
591  int right = range.right();
592  int other_top = other.top();
593  int other_left = other.left();
594  int other_bottom = other.bottom();
595  int other_right = other.right();
596  const QAbstractItemModel *model = range.model();
597  Q_ASSERT(model);
598  if (other_top > top) {
599  QModelIndex tl = model->index(top, left, parent);
600  QModelIndex br = model->index(other_top - 1, right, parent);
601  result->append(QItemSelectionRange(tl, br));
602  top = other_top;
603  }
604  if (other_bottom < bottom) {
605  QModelIndex tl = model->index(other_bottom + 1, left, parent);
606  QModelIndex br = model->index(bottom, right, parent);
607  result->append(QItemSelectionRange(tl, br));
608  bottom = other_bottom;
609  }
610  if (other_left > left) {
611  QModelIndex tl = model->index(top, left, parent);
612  QModelIndex br = model->index(bottom, other_left - 1, parent);
613  result->append(QItemSelectionRange(tl, br));
614  left = other_left;
615  }
616  if (other_right < right) {
617  QModelIndex tl = model->index(top, other_right + 1, parent);
618  QModelIndex br = model->index(bottom, right, parent);
619  result->append(QItemSelectionRange(tl, br));
620  right = other_right;
621  }
622 }
int left() const
Returns the column index corresponding to the leftmost selected column in the selection range...
The QItemSelectionRange class manages information about a range of selected items in a model...
int bottom() const
Returns the row index corresponding to the lowermost selected row in the selection range...
int right() const
Returns the column index corresponding to the rightmost selected column in the selection range...
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QModelIndex parent() const
Returns the parent model item index of the items in the selection range.
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const =0
Returns the index of the item in the model specified by the given row, column and parent index...
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QAbstractItemModel class provides the abstract interface for item model classes.
The QModelIndex class is used to locate data in a data model.
int top() const
Returns the row index corresponding to the uppermost selected row in the selection range...
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
const QAbstractItemModel * model() const
Returns the model that the items in the selection range belong to.

The documentation for this class was generated from the following files: