Qt 4.8
Public Types | Public Functions | Private Functions | Properties | Friends | List of all members
QTreeWidgetItemIterator Class Reference

The QTreeWidgetItemIterator class provides a way to iterate over the items in a QTreeWidget instance. More...

#include <qtreewidgetitemiterator.h>

Public Types

enum  IteratorFlag {
  All = 0x00000000, Hidden = 0x00000001, NotHidden = 0x00000002, Selected = 0x00000004,
  Unselected = 0x00000008, Selectable = 0x00000010, NotSelectable = 0x00000020, DragEnabled = 0x00000040,
  DragDisabled = 0x00000080, DropEnabled = 0x00000100, DropDisabled = 0x00000200, HasChildren = 0x00000400,
  NoChildren = 0x00000800, Checked = 0x00001000, NotChecked = 0x00002000, Enabled = 0x00004000,
  Disabled = 0x00008000, Editable = 0x00010000, NotEditable = 0x00020000, UserFlag = 0x01000000
}
 These flags can be passed to a QTreeWidgetItemIterator constructor (OR-ed together if more than one is used), so that the iterator will only iterate over items that match the given flags. More...
 

Public Functions

QTreeWidgetItemoperator* () const
 Dereference operator. More...
 
QTreeWidgetItemIteratoroperator++ ()
 The prefix ++ operator (++it) advances the iterator to the next matching item and returns a reference to the resulting iterator. More...
 
const QTreeWidgetItemIterator operator++ (int)
 The postfix ++ operator (it++) advances the iterator to the next matching item and returns an iterator to the previously current item. More...
 
QTreeWidgetItemIteratoroperator+= (int n)
 Makes the iterator go forward by n matching items. More...
 
QTreeWidgetItemIteratoroperator-- ()
 The prefix – operator (–it) advances the iterator to the previous matching item and returns a reference to the resulting iterator. More...
 
const QTreeWidgetItemIterator operator-- (int)
 The postfix – operator (it–) makes the preceding matching item current and returns an iterator to the previously current item. More...
 
QTreeWidgetItemIteratoroperator-= (int n)
 Makes the iterator go backward by n matching items. More...
 
QTreeWidgetItemIteratoroperator= (const QTreeWidgetItemIterator &it)
 Assignment. More...
 
 QTreeWidgetItemIterator (const QTreeWidgetItemIterator &it)
 Constructs an iterator for the same QTreeWidget as it. More...
 
 QTreeWidgetItemIterator (QTreeWidget *widget, IteratorFlags flags=All)
 Constructs an iterator for the given widget that uses the specified flags to determine which items are found during iteration. More...
 
 QTreeWidgetItemIterator (QTreeWidgetItem *item, IteratorFlags flags=All)
 Constructs an iterator for the given item that uses the specified flags to determine which items are found during iteration. More...
 
 ~QTreeWidgetItemIterator ()
 Destroys the iterator. More...
 

Private Functions

bool matchesFlags (const QTreeWidgetItem *item) const
 

Properties

QTreeWidgetItemcurrent
 
QScopedPointer< QTreeWidgetItemIteratorPrivated_ptr
 
IteratorFlags flags
 

Friends

class QTreeModel
 

Detailed Description

The QTreeWidgetItemIterator class provides a way to iterate over the items in a QTreeWidget instance.

The iterator will walk the items in a pre-order traversal order, thus visiting the parent node before it continues to the child nodes.

For example, the following code examples each item in a tree, checking the text in the first column against a user-specified search string:

while (*it) {
if ((*it)->text(0) == itemText)
(*it)->setSelected(true);
++it;
}

It is also possible to filter out certain types of node by passing certain flags to the constructor of QTreeWidgetItemIterator.

See also
QTreeWidget, {Model/View Programming}, QTreeWidgetItem

Definition at line 61 of file qtreewidgetitemiterator.h.

Enumerations

◆ IteratorFlag

These flags can be passed to a QTreeWidgetItemIterator constructor (OR-ed together if more than one is used), so that the iterator will only iterate over items that match the given flags.

  • All
  • Hidden
  • NotHidden
  • Selected
  • Unselected
  • Selectable
  • NotSelectable
  • DragEnabled
  • DragDisabled
  • DropEnabled
  • DropDisabled
  • HasChildren
  • NoChildren
  • Checked
  • NotChecked
  • Enabled
  • Disabled
  • Editable
  • NotEditable
  • UserFlag
Enumerator
All 
Hidden 
NotHidden 
Selected 
Unselected 
Selectable 
NotSelectable 
DragEnabled 
DragDisabled 
DropEnabled 
DropDisabled 
HasChildren 
NoChildren 
Checked 
NotChecked 
Enabled 
Disabled 
Editable 
NotEditable 
UserFlag 

Definition at line 66 of file qtreewidgetitemiterator.h.

66  {
67  All = 0x00000000,
68  Hidden = 0x00000001,
69  NotHidden = 0x00000002,
70  Selected = 0x00000004,
71  Unselected = 0x00000008,
72  Selectable = 0x00000010,
73  NotSelectable = 0x00000020,
74  DragEnabled = 0x00000040,
75  DragDisabled = 0x00000080,
76  DropEnabled = 0x00000100,
77  DropDisabled = 0x00000200,
78  HasChildren = 0x00000400,
79  NoChildren = 0x00000800,
80  Checked = 0x00001000,
81  NotChecked = 0x00002000,
82  Enabled = 0x00004000,
83  Disabled = 0x00008000,
84  Editable = 0x00010000,
85  NotEditable = 0x00020000,
86  UserFlag = 0x01000000 // The first flag that can be used by the user.
87  };

Constructors and Destructors

◆ QTreeWidgetItemIterator() [1/3]

QTreeWidgetItemIterator::QTreeWidgetItemIterator ( const QTreeWidgetItemIterator it)

Constructs an iterator for the same QTreeWidget as it.

The current iterator item is set to point on the current item of it.

Definition at line 79 of file qtreewidgetitemiterator.cpp.

Referenced by QTreeWidgetItemIteratorPrivate::ensureValidIterator().

81  current(it.current), flags(it.flags)
82 {
84  Q_ASSERT(d->m_model);
85  d->m_model->iterators.append(this);
86 }
double d
Definition: qnumeric_p.h:62
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
QScopedPointer< QTreeWidgetItemIteratorPrivate > d_ptr
The QTreeWidgetItemIterator class provides a way to iterate over the items in a QTreeWidget instance...

◆ QTreeWidgetItemIterator() [2/3]

QTreeWidgetItemIterator::QTreeWidgetItemIterator ( QTreeWidget widget,
IteratorFlags  flags = All 
)
explicit

Constructs an iterator for the given widget that uses the specified flags to determine which items are found during iteration.

The iterator is set to point to the first top-level item contained in the widget, or the next matching item if the top-level item doesn't match the flags.

See also
QTreeWidgetItemIterator::IteratorFlag

Definition at line 97 of file qtreewidgetitemiterator.cpp.

98 : current(0), flags(flags)
99 {
100  Q_ASSERT(widget);
101  QTreeModel *model = qobject_cast<QTreeModel*>(widget->model());
102  Q_ASSERT(model);
103  d_ptr.reset(new QTreeWidgetItemIteratorPrivate(this, model));
104  model->iterators.append(this);
105  if (!model->rootItem->children.isEmpty()) current = model->rootItem->child(0);
106  if (current && !matchesFlags(current))
107  ++(*this);
108 }
T qobject_cast(QObject *object)
Definition: qobject.h:375
bool matchesFlags(const QTreeWidgetItem *item) const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QScopedPointer< QTreeWidgetItemIteratorPrivate > d_ptr
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
QList< QTreeWidgetItemIterator * > iterators
QTreeWidgetItem * rootItem
void reset(T *other=0)
Deletes the existing object it is pointing to if any, and sets its pointer to other.
QTreeWidgetItem * child(int index) const
Returns the item at the given index in the list of the item&#39;s children.
Definition: qtreewidget.h:184
QList< QTreeWidgetItem * > children
Definition: qtreewidget.h:222
QAbstractItemModel * model() const
Returns the model that this view is presenting.

◆ QTreeWidgetItemIterator() [3/3]

QTreeWidgetItemIterator::QTreeWidgetItemIterator ( QTreeWidgetItem item,
IteratorFlags  flags = All 
)
explicit

Constructs an iterator for the given item that uses the specified flags to determine which items are found during iteration.

The iterator is set to point to item, or the next matching item if item doesn't match the flags.

See also
QTreeWidgetItemIterator::IteratorFlag

Definition at line 119 of file qtreewidgetitemiterator.cpp.

121  this, qobject_cast<QTreeModel*>(item->view->model()))),
122  current(item), flags(flags)
123 {
125  Q_ASSERT(item);
126  QTreeModel *model = qobject_cast<QTreeModel*>(item->view->model());
127  Q_ASSERT(model);
128  model->iterators.append(this);
129 
130  // Initialize m_currentIndex and m_parentIndex as it would be if we had traversed from
131  // the beginning.
132  QTreeWidgetItem *parent = item;
133  parent = parent->parent();
134  QTreeWidgetItem *root = d->m_model->rootItem;
135  d->m_currentIndex = (parent ? parent : root)->indexOfChild(item);
136 
137  while (parent) {
138  QTreeWidgetItem *itm = parent;
139  parent = parent->parent();
140  const int index = (parent ? parent : root)->indexOfChild(itm);
141  d->m_parentIndex.prepend(index);
142  }
143 
144  if (current && !matchesFlags(current))
145  ++(*this);
146 }
T qobject_cast(QObject *object)
Definition: qobject.h:375
double d
Definition: qnumeric_p.h:62
bool matchesFlags(const QTreeWidgetItem *item) const
QTreeWidget * view
Definition: qtreewidget.h:219
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
QScopedPointer< QTreeWidgetItemIteratorPrivate > d_ptr
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QTreeWidgetItemIterator class provides a way to iterate over the items in a QTreeWidget instance...
QList< QTreeWidgetItemIterator * > iterators
QTreeWidgetItem * parent() const
Returns the item&#39;s parent.
Definition: qtreewidget.h:183
The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class...
Definition: qtreewidget.h:63
quint16 index
QAbstractItemModel * model() const
Returns the model that this view is presenting.

◆ ~QTreeWidgetItemIterator()

QTreeWidgetItemIterator::~QTreeWidgetItemIterator ( )

Destroys the iterator.

Definition at line 152 of file qtreewidgetitemiterator.cpp.

153 {
154  d_func()->m_model->iterators.removeAll(this);
155 }

Functions

◆ matchesFlags()

bool QTreeWidgetItemIterator::matchesFlags ( const QTreeWidgetItem item) const
private
Warning
This function is not part of the public interface.

Definition at line 208 of file qtreewidgetitemiterator.cpp.

Referenced by operator++(), operator--(), and QTreeWidgetItemIterator().

209 {
210  if (!item)
211  return false;
212 
213  if (flags == All)
214  return true;
215 
216  {
217  Qt::ItemFlags itemFlags = item->flags();
218  if ((flags & Selectable) && !(itemFlags & Qt::ItemIsSelectable))
219  return false;
220  if ((flags & NotSelectable) && (itemFlags & Qt::ItemIsSelectable))
221  return false;
222  if ((flags & DragEnabled) && !(itemFlags & Qt::ItemIsDragEnabled))
223  return false;
224  if ((flags & DragDisabled) && (itemFlags & Qt::ItemIsDragEnabled))
225  return false;
226  if ((flags & DropEnabled) && !(itemFlags & Qt::ItemIsDropEnabled))
227  return false;
228  if ((flags & DropDisabled) && (itemFlags & Qt::ItemIsDropEnabled))
229  return false;
230  if ((flags & Enabled) && !(itemFlags & Qt::ItemIsEnabled))
231  return false;
232  if ((flags & Disabled) && (itemFlags & Qt::ItemIsEnabled))
233  return false;
234  if ((flags & Editable) && !(itemFlags & Qt::ItemIsEditable))
235  return false;
236  if ((flags & NotEditable) && (itemFlags & Qt::ItemIsEditable))
237  return false;
238  }
239 
240  if (flags & (Checked|NotChecked)) {
241  // ### We only test the check state for column 0
242  Qt::CheckState check = item->checkState(0);
243  // PartiallyChecked matches as Checked.
244  if ((flags & Checked) && (check == Qt::Unchecked))
245  return false;
246  if ((flags & NotChecked) && (check != Qt::Unchecked))
247  return false;
248  }
249 
250  if ((flags & HasChildren) && !item->childCount())
251  return false;
252  if ((flags & NoChildren) && item->childCount())
253  return false;
254 
255  if ((flags & Hidden) && !item->isHidden())
256  return false;
257  if ((flags & NotHidden) && item->isHidden())
258  return false;
259 
260  if ((flags & Selected) && !item->isSelected())
261  return false;
262  if ((flags & Unselected) && item->isSelected())
263  return false;
264 
265  return true;
266 }
CheckState
Definition: qnamespace.h:1607
Qt::ItemFlags flags() const
Returns the flags used to describe the item.
bool isHidden() const
Returns true if the item is hidden, otherwise returns false.
Definition: qtreewidget.h:405
int childCount() const
Returns the number of child items.
Definition: qtreewidget.h:190
Qt::CheckState checkState(int column) const
Returns the check state of the label in the given column.
Definition: qtreewidget.h:162
bool isSelected() const
Returns true if the item is selected, otherwise returns false.
Definition: qtreewidget.h:399

◆ operator*()

QTreeWidgetItem * QTreeWidgetItemIterator::operator* ( ) const
inline

Dereference operator.

Returns a pointer to the current item.

Definition at line 147 of file qtreewidgetitemiterator.h.

148 {
149  return current;
150 }

◆ operator++() [1/2]

QTreeWidgetItemIterator & QTreeWidgetItemIterator::operator++ ( )

The prefix ++ operator (++it) advances the iterator to the next matching item and returns a reference to the resulting iterator.

Sets the current pointer to 0 if the current item is the last matching item.

Definition at line 181 of file qtreewidgetitemiterator.cpp.

182 {
183  if (current)
184  do {
185  current = d_func()->next(current);
186  } while (current && !matchesFlags(current));
187  return *this;
188 }
bool matchesFlags(const QTreeWidgetItem *item) const

◆ operator++() [2/2]

const QTreeWidgetItemIterator QTreeWidgetItemIterator::operator++ ( int  )
inline

The postfix ++ operator (it++) advances the iterator to the next matching item and returns an iterator to the previously current item.

Definition at line 115 of file qtreewidgetitemiterator.h.

116 {
117  QTreeWidgetItemIterator it = *this;
118  ++(*this);
119  return it;
120 }
#define it(className, varName)
The QTreeWidgetItemIterator class provides a way to iterate over the items in a QTreeWidget instance...

◆ operator+=()

QTreeWidgetItemIterator & QTreeWidgetItemIterator::operator+= ( int  n)
inline

Makes the iterator go forward by n matching items.

(If n is negative, the iterator goes backward.)

If the current item is beyond the last item, the current item pointer is set to 0. Returns the resulting iterator.

Definition at line 129 of file qtreewidgetitemiterator.h.

130 {
131  if (n < 0)
132  return (*this) -= (-n);
133  while (current && n--)
134  ++(*this);
135  return *this;
136 }

◆ operator--() [1/2]

QTreeWidgetItemIterator & QTreeWidgetItemIterator::operator-- ( )

The prefix – operator (–it) advances the iterator to the previous matching item and returns a reference to the resulting iterator.

Sets the current pointer to 0 if the current item is the first matching item.

Definition at line 196 of file qtreewidgetitemiterator.cpp.

197 {
198  if (current)
199  do {
200  current = d_func()->previous(current);
201  } while (current && !matchesFlags(current));
202  return *this;
203 }
bool matchesFlags(const QTreeWidgetItem *item) const

◆ operator--() [2/2]

const QTreeWidgetItemIterator QTreeWidgetItemIterator::operator-- ( int  )
inline

The postfix – operator (it–) makes the preceding matching item current and returns an iterator to the previously current item.

Definition at line 122 of file qtreewidgetitemiterator.h.

123 {
124  QTreeWidgetItemIterator it = *this;
125  --(*this);
126  return it;
127 }
#define it(className, varName)
The QTreeWidgetItemIterator class provides a way to iterate over the items in a QTreeWidget instance...

◆ operator-=()

QTreeWidgetItemIterator & QTreeWidgetItemIterator::operator-= ( int  n)
inline

Makes the iterator go backward by n matching items.

(If n is negative, the iterator goes forward.)

If the current item is ahead of the last item, the current item pointer is set to 0. Returns the resulting iterator.

Definition at line 138 of file qtreewidgetitemiterator.h.

139 {
140  if (n < 0)
141  return (*this) += (-n);
142  while (current && n--)
143  --(*this);
144  return *this;
145 }

◆ operator=()

QTreeWidgetItemIterator & QTreeWidgetItemIterator::operator= ( const QTreeWidgetItemIterator it)

Assignment.

Makes a copy of it and returns a reference to its iterator.

Definition at line 162 of file qtreewidgetitemiterator.cpp.

163 {
165  if (d_func()->m_model != it.d_func()->m_model) {
166  d_func()->m_model->iterators.removeAll(this);
167  it.d_func()->m_model->iterators.append(this);
168  }
169  current = it.current;
170  flags = it.flags;
171  d->operator=(*it.d_func());
172  return *this;
173 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QTreeWidgetItemIterator class provides a way to iterate over the items in a QTreeWidget instance...

Friends and Related Functions

◆ QTreeModel

friend class QTreeModel
friend

Definition at line 63 of file qtreewidgetitemiterator.h.

Properties

◆ current

QTreeWidgetItem* QTreeWidgetItemIterator::current
private

◆ d_ptr

QScopedPointer<QTreeWidgetItemIteratorPrivate> QTreeWidgetItemIterator::d_ptr
private

Definition at line 109 of file qtreewidgetitemiterator.h.

Referenced by QTreeWidgetItemIterator().

◆ flags

IteratorFlags QTreeWidgetItemIterator::flags
private

Definition at line 111 of file qtreewidgetitemiterator.h.

Referenced by matchesFlags(), and operator=().


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