- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# qtreewidget.h

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    00004 ** All rights reserved.
    00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
    00006 **
    00007 ** This file is part of the QtGui module of the Qt Toolkit.
    00008 **
    00009 ** $QT_BEGIN_LICENSE:LGPL$
    00010 ** Commercial Usage
    00011 ** Licensees holding valid Qt Commercial licenses may use this file in
    00012 ** accordance with the Qt Commercial License Agreement provided with the
    00013 ** Software or, alternatively, in accordance with the terms contained in
    00014 ** a written agreement between you and Nokia.
    00015 **
    00016 ** GNU Lesser General Public License Usage
    00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
    00018 ** General Public License version 2.1 as published by the Free Software
    00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
    00020 ** packaging of this file.  Please review the following information to
    00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
    00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    00023 **
    00024 ** In addition, as a special exception, Nokia gives you certain additional
    00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
    00027 **
    00028 ** GNU General Public License Usage
    00029 ** Alternatively, this file may be used under the terms of the GNU
    00030 ** General Public License version 3.0 as published by the Free Software
    00031 ** Foundation and appearing in the file LICENSE.GPL included in the
    00032 ** packaging of this file.  Please review the following information to
    00033 ** ensure the GNU General Public License version 3.0 requirements will be
    00034 ** met: http://www.gnu.org/copyleft/gpl.html.
    00035 **
    00036 ** If you have questions regarding the use of this file, please contact
    00037 ** Nokia at qt-info@nokia.com.
    00038 ** $QT_END_LICENSE$
    00039 **
    00040 ****************************************************************************/
    00041
    00042 #ifndef QTREEWIDGET_H
    00043 #define QTREEWIDGET_H
    00044
    00045 #include <QtGui/qtreeview.h>
    00046 #include <QtGui/qtreewidgetitemiterator.h>
    00047 #include <QtCore/qvariant.h>
    00048 #include <QtCore/qvector.h>
    00049
    00050 QT_BEGIN_HEADER
    00051
    00052 QT_BEGIN_NAMESPACE
    00053
    00054 QT_MODULE(Gui)
    00055
    00056 #ifndef QT_NO_TREEWIDGET
    00057
    00058 class QTreeWidget;
    00059 class QTreeModel;
    00060 class QWidgetItemData;
    00061 class QTreeWidgetItemPrivate;
    00062
    00063 class Q_GUI_EXPORT QTreeWidgetItem
    00064 {
    00065     friend class QTreeModel;
    00066     friend class QTreeWidget;
    00067     friend class QTreeWidgetPrivate;
    00068     friend class QTreeWidgetItemIterator;
    00069     friend class QTreeWidgetItemPrivate;
    00070 public:
    00071     enum ItemType { Type = 0, UserType = 1000 };
    00072     explicit QTreeWidgetItem(int type = Type);
    00073     QTreeWidgetItem(const QStringList &strings, int type = Type);
    00074     explicit QTreeWidgetItem(QTreeWidget *view, int type = Type);
    00075     QTreeWidgetItem(QTreeWidget *view, const QStringList &strings, int type = Type);
    00076     QTreeWidgetItem(QTreeWidget *view, QTreeWidgetItem *after, int type = Type);
    00077     explicit QTreeWidgetItem(QTreeWidgetItem *parent, int type = Type);
    00078     QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type = Type);
    00079     QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type = Type);
    00080     QTreeWidgetItem(const QTreeWidgetItem &other);
    00081     virtual ~QTreeWidgetItem();
    00082
    00083     virtual QTreeWidgetItem *clone() const;
    00084
    00085     inline QTreeWidget *treeWidget() const { return view; }
    00086
    00087     inline void setSelected(bool select);
    00088     inline bool isSelected() const;
    00089
    00090     inline void setHidden(bool hide);
    00091     inline bool isHidden() const;
    00092
    00093     inline void setExpanded(bool expand);
    00094     inline bool isExpanded() const;
    00095
    00096     inline void setFirstColumnSpanned(bool span);
    00097     inline bool isFirstColumnSpanned() const;
    00098
    00099     inline void setDisabled(bool disabled);
    00100     inline bool isDisabled() const;
    00101
    00102     enum ChildIndicatorPolicy { ShowIndicator, DontShowIndicator, DontShowIndicatorWhenChildless };
    00103     void setChildIndicatorPolicy(QTreeWidgetItem::ChildIndicatorPolicy policy);
    00104     QTreeWidgetItem::ChildIndicatorPolicy childIndicatorPolicy() const;
    00105
    00106     Qt::ItemFlags flags() const;
    00107     void setFlags(Qt::ItemFlags flags);
    00108
    00109     inline QString text(int column) const
    00110         { return data(column, Qt::DisplayRole).toString(); }
    00111     inline void setText(int column, const QString &text);
    00112
    00113     inline QIcon icon(int column) const
    00114         { return qvariant_cast<QIcon>(data(column, Qt::DecorationRole)); }
    00115     inline void setIcon(int column, const QIcon &icon);
    00116
    00117     inline QString statusTip(int column) const
    00118         { return data(column, Qt::StatusTipRole).toString(); }
    00119     inline void setStatusTip(int column, const QString &statusTip);
    00120
    00121 #ifndef QT_NO_TOOLTIP
    00122     inline QString toolTip(int column) const
    00123         { return data(column, Qt::ToolTipRole).toString(); }
    00124     inline void setToolTip(int column, const QString &toolTip);
    00125 #endif
    00126
    00127 #ifndef QT_NO_WHATSTHIS
    00128     inline QString whatsThis(int column) const
    00129         { return data(column, Qt::WhatsThisRole).toString(); }
    00130     inline void setWhatsThis(int column, const QString &whatsThis);
    00131 #endif
    00132
    00133     inline QFont font(int column) const
    00134         { return qvariant_cast<QFont>(data(column, Qt::FontRole)); }
    00135     inline void setFont(int column, const QFont &font);
    00136
    00137     inline int textAlignment(int column) const
    00138         { return data(column, Qt::TextAlignmentRole).toInt(); }
    00139     inline void setTextAlignment(int column, int alignment)
    00140         { setData(column, Qt::TextAlignmentRole, alignment); }
    00141
    00142     inline QColor backgroundColor(int column) const
    00143         { return qvariant_cast<QColor>(data(column, Qt::BackgroundColorRole)); }
    00144     inline void setBackgroundColor(int column, const QColor &color)
    00145         { setData(column, Qt::BackgroundColorRole, color); }
    00146
    00147     inline QBrush background(int column) const
    00148         { return qvariant_cast<QBrush>(data(column, Qt::BackgroundRole)); }
    00149     inline void setBackground(int column, const QBrush &brush)
    00150         { setData(column, Qt::BackgroundRole, brush); }
    00151
    00152     inline QColor textColor(int column) const
    00153         { return qvariant_cast<QColor>(data(column, Qt::TextColorRole)); }
    00154     inline void setTextColor(int column, const QColor &color)
    00155         { setData(column, Qt::TextColorRole, color); }
    00156
    00157     inline QBrush foreground(int column) const
    00158         { return qvariant_cast<QBrush>(data(column, Qt::ForegroundRole)); }
    00159     inline void setForeground(int column, const QBrush &brush)
    00160         { setData(column, Qt::ForegroundRole, brush); }
    00161
    00162     inline Qt::CheckState checkState(int column) const
    00163         { return static_cast<Qt::CheckState>(data(column, Qt::CheckStateRole).toInt()); }
    00164     inline void setCheckState(int column, Qt::CheckState state)
    00165         { setData(column, Qt::CheckStateRole, state); }
    00166
    00167     inline QSize sizeHint(int column) const
    00168         { return qvariant_cast<QSize>(data(column, Qt::SizeHintRole)); }
    00169     inline void setSizeHint(int column, const QSize &size)
    00170         { setData(column, Qt::SizeHintRole, size); }
    00171
    00172     virtual QVariant data(int column, int role) const;
    00173     virtual void setData(int column, int role, const QVariant &value);
    00174
    00175     virtual bool operator<(const QTreeWidgetItem &other) const;
    00176
    00177 #ifndef QT_NO_DATASTREAM
    00178     virtual void read(QDataStream &in);
    00179     virtual void write(QDataStream &out) const;
    00180 #endif
    00181     QTreeWidgetItem &operator=(const QTreeWidgetItem &other);
    00182
    00183     inline QTreeWidgetItem *parent() const { return par; }
    00184     inline QTreeWidgetItem *child(int index) const {
    00185         if (index < 0 || index >= children.size())
    00186             return 0;
    00187         executePendingSort();
    00188         return children.at(index);
    00189     }
    00190     inline int childCount() const { return children.count(); }
    00191     inline int columnCount() const { return values.count(); }
    00192     inline int indexOfChild(QTreeWidgetItem *child) const;
    00193
    00194     void addChild(QTreeWidgetItem *child);
    00195     void insertChild(int index, QTreeWidgetItem *child);
    00196     void removeChild(QTreeWidgetItem *child);
    00197     QTreeWidgetItem *takeChild(int index);
    00198
    00199     void addChildren(const QList<QTreeWidgetItem*> &children);
    00200     void insertChildren(int index, const QList<QTreeWidgetItem*> &children);
    00201     QList<QTreeWidgetItem*> takeChildren();
    00202
    00203     inline int type() const { return rtti; }
    00204     inline void sortChildren(int column, Qt::SortOrder order)
    00205         { sortChildren(column, order, false); }
    00206
    00207 protected:
    00208     void emitDataChanged();
    00209
    00210 private:
    00211     void sortChildren(int column, Qt::SortOrder order, bool climb);
    00212     QVariant childrenCheckState(int column) const;
    00213     void itemChanged();
    00214     void executePendingSort() const;
    00215
    00216     int rtti;
    00217     // One item has a vector of column entries. Each column has a vector of (role, value) pairs.
    00218     QVector< QVector<QWidgetItemData> > values;
    00219     QTreeWidget *view;
    00220     QTreeWidgetItemPrivate *d;
    00221     QTreeWidgetItem *par;
    00222     QList<QTreeWidgetItem*> children;
    00223     Qt::ItemFlags itemFlags;
    00224 };
    00225
    00226 inline void QTreeWidgetItem::setText(int column, const QString &atext)
    00227 { setData(column, Qt::DisplayRole, atext); }
    00228
    00229 inline void QTreeWidgetItem::setIcon(int column, const QIcon &aicon)
    00230 { setData(column, Qt::DecorationRole, aicon); }
    00231
    00232 #ifndef QT_NO_STATUSTIP
    00233 inline void QTreeWidgetItem::setStatusTip(int column, const QString &astatusTip)
    00234 { setData(column, Qt::StatusTipRole, astatusTip); }
    00235 #endif
    00236
    00237 #ifndef QT_NO_TOOLTIP
    00238 inline void QTreeWidgetItem::setToolTip(int column, const QString &atoolTip)
    00239 { setData(column, Qt::ToolTipRole, atoolTip); }
    00240 #endif
    00241
    00242 #ifndef QT_NO_WHATSTHIS
    00243 inline void QTreeWidgetItem::setWhatsThis(int column, const QString &awhatsThis)
    00244 { setData(column, Qt::WhatsThisRole, awhatsThis); }
    00245 #endif
    00246
    00247 inline void QTreeWidgetItem::setFont(int column, const QFont &afont)
    00248 { setData(column, Qt::FontRole, afont); }
    00249
    00250 inline int QTreeWidgetItem::indexOfChild(QTreeWidgetItem *achild) const
    00251 { executePendingSort(); return children.indexOf(achild); }
    00252
    00253 #ifndef QT_NO_DATASTREAM
    00254 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &out, const QTreeWidgetItem &item);
    00255 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &in, QTreeWidgetItem &item);
    00256 #endif
    00257
    00258 class QTreeWidgetPrivate;
    00259
    00260 class Q_GUI_EXPORT QTreeWidget : public QTreeView
    00261 {
    00262     Q_OBJECT
    00263     Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount)
    00264     Q_PROPERTY(int topLevelItemCount READ topLevelItemCount)
    00265
    00266     friend class QTreeModel;
    00267     friend class QTreeWidgetItem;
    00268 public:
    00269     explicit QTreeWidget(QWidget *parent = 0);
    00270     ~QTreeWidget();
    00271
    00272     int columnCount() const;
    00273     void setColumnCount(int columns);
    00274
    00275     QTreeWidgetItem *invisibleRootItem() const;
    00276     QTreeWidgetItem *topLevelItem(int index) const;
    00277     int topLevelItemCount() const;
    00278     void insertTopLevelItem(int index, QTreeWidgetItem *item);
    00279     void addTopLevelItem(QTreeWidgetItem *item);
    00280     QTreeWidgetItem *takeTopLevelItem(int index);
    00281     int indexOfTopLevelItem(QTreeWidgetItem *item); // ### Qt 5: remove me
    00282     int indexOfTopLevelItem(QTreeWidgetItem *item) const;
    00283
    00284     void insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items);
    00285     void addTopLevelItems(const QList<QTreeWidgetItem*> &items);
    00286
    00287     QTreeWidgetItem *headerItem() const;
    00288     void setHeaderItem(QTreeWidgetItem *item);
    00289     void setHeaderLabels(const QStringList &labels);
    00290     inline void setHeaderLabel(const QString &label);
    00291
    00292     QTreeWidgetItem *currentItem() const;
    00293     int currentColumn() const;
    00294     void setCurrentItem(QTreeWidgetItem *item);
    00295     void setCurrentItem(QTreeWidgetItem *item, int column);
    00296     void setCurrentItem(QTreeWidgetItem *item, int column, QItemSelectionModel::SelectionFlags command);
    00297
    00298     QTreeWidgetItem *itemAt(const QPoint &p) const;
    00299     inline QTreeWidgetItem *itemAt(int x, int y) const;
    00300     QRect visualItemRect(const QTreeWidgetItem *item) const;
    00301
    00302     int sortColumn() const;
    00303     void sortItems(int column, Qt::SortOrder order);
    00304     void setSortingEnabled(bool enable);
    00305     bool isSortingEnabled() const;
    00306
    00307     void editItem(QTreeWidgetItem *item, int column = 0);
    00308     void openPersistentEditor(QTreeWidgetItem *item, int column = 0);
    00309     void closePersistentEditor(QTreeWidgetItem *item, int column = 0);
    00310
    00311     QWidget *itemWidget(QTreeWidgetItem *item, int column) const;
    00312     void setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget);
    00313     inline void removeItemWidget(QTreeWidgetItem *item, int column);
    00314
    00315     bool isItemSelected(const QTreeWidgetItem *item) const;
    00316     void setItemSelected(const QTreeWidgetItem *item, bool select);
    00317     QList<QTreeWidgetItem*> selectedItems() const;
    00318     QList<QTreeWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags,
    00319                                       int column = 0) const;
    00320
    00321     bool isItemHidden(const QTreeWidgetItem *item) const;
    00322     void setItemHidden(const QTreeWidgetItem *item, bool hide);
    00323
    00324     bool isItemExpanded(const QTreeWidgetItem *item) const;
    00325     void setItemExpanded(const QTreeWidgetItem *item, bool expand);
    00326
    00327     bool isFirstItemColumnSpanned(const QTreeWidgetItem *item) const;
    00328     void setFirstItemColumnSpanned(const QTreeWidgetItem *item, bool span);
    00329
    00330     QTreeWidgetItem *itemAbove(const QTreeWidgetItem *item) const;
    00331     QTreeWidgetItem *itemBelow(const QTreeWidgetItem *item) const;
    00332
    00333     void setSelectionModel(QItemSelectionModel *selectionModel);
    00334
    00335 public Q_SLOTS:
    00336     void scrollToItem(const QTreeWidgetItem *item,
    00337                       QAbstractItemView::ScrollHint hint = EnsureVisible);
    00338     void expandItem(const QTreeWidgetItem *item);
    00339     void collapseItem(const QTreeWidgetItem *item);
    00340     void clear();
    00341
    00342 Q_SIGNALS:
    00343     void itemPressed(QTreeWidgetItem *item, int column);
    00344     void itemClicked(QTreeWidgetItem *item, int column);
    00345     void itemDoubleClicked(QTreeWidgetItem *item, int column);
    00346     void itemActivated(QTreeWidgetItem *item, int column);
    00347     void itemEntered(QTreeWidgetItem *item, int column);
    00348     void itemChanged(QTreeWidgetItem *item, int column);
    00349     void itemExpanded(QTreeWidgetItem *item);
    00350     void itemCollapsed(QTreeWidgetItem *item);
    00351     void currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
    00352     void itemSelectionChanged();
    00353
    00354 protected:
    00355     bool event(QEvent *e);
    00356     virtual QStringList mimeTypes() const;
    00357     virtual QMimeData *mimeData(const QList<QTreeWidgetItem*> items) const;
    00358     virtual bool dropMimeData(QTreeWidgetItem *parent, int index,
    00359                               const QMimeData *data, Qt::DropAction action);
    00360     virtual Qt::DropActions supportedDropActions() const;
    00361     QList<QTreeWidgetItem*> items(const QMimeData *data) const;
    00362
    00363     QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const;
    00364     QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const;
    00365     void dropEvent(QDropEvent *event);
    00366
    00367 private:
    00368     void setModel(QAbstractItemModel *model);
    00369
    00370     Q_DECLARE_PRIVATE(QTreeWidget)
    00371     Q_DISABLE_COPY(QTreeWidget)
    00372
    00373     Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
    00374     Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
    00375     Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
    00376     Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
    00377     Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
    00378     Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
    00379     Q_PRIVATE_SLOT(d_func(), void _q_emitItemExpanded(const QModelIndex &index))
    00380     Q_PRIVATE_SLOT(d_func(), void _q_emitItemCollapsed(const QModelIndex &index))
    00381     Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
    00382     Q_PRIVATE_SLOT(d_func(), void _q_sort())
    00383     Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
    00384     Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected))
    00385 };
    00386
    00387 inline void QTreeWidget::removeItemWidget(QTreeWidgetItem *item, int column)
    00388 { setItemWidget(item, column, 0); }
    00389
    00390 inline QTreeWidgetItem *QTreeWidget::itemAt(int ax, int ay) const
    00391 { return itemAt(QPoint(ax, ay)); }
    00392
    00393 inline void QTreeWidget::setHeaderLabel(const QString &alabel)
    00394 { setHeaderLabels(QStringList(alabel)); }
    00395
    00396 inline void QTreeWidgetItem::setSelected(bool aselect)
    00397 { if (view) view->setItemSelected(this, aselect); }
    00398
    00399 inline bool QTreeWidgetItem::isSelected() const
    00400 { return (view ? view->isItemSelected(this) : false); }
    00401
    00402 inline void QTreeWidgetItem::setHidden(bool ahide)
    00403 { if (view) view->setItemHidden(this, ahide); }
    00404
    00405 inline bool QTreeWidgetItem::isHidden() const
    00406 { return (view ? view->isItemHidden(this) : false); }
    00407
    00408 inline void QTreeWidgetItem::setExpanded(bool aexpand)
    00409 { if (view) view->setItemExpanded(this, aexpand); }
    00410
    00411 inline bool QTreeWidgetItem::isExpanded() const
    00412 { return (view ? view->isItemExpanded(this) : false); }
    00413
    00414 inline void QTreeWidgetItem::setFirstColumnSpanned(bool aspan)
    00415 { if (view) view->setFirstItemColumnSpanned(this, aspan); }
    00416
    00417 inline bool QTreeWidgetItem::isFirstColumnSpanned() const
    00418 { return (view ? view->isFirstItemColumnSpanned(this) : false); }
    00419
    00420 inline void QTreeWidgetItem::setDisabled(bool disabled)
    00421 { setFlags(disabled ? (flags() & ~Qt::ItemIsEnabled) : flags() | Qt::ItemIsEnabled); }
    00422
    00423 inline bool QTreeWidgetItem::isDisabled() const
    00424 { return !(flags() & Qt::ItemIsEnabled); }
    00425
    00426 #endif // QT_NO_TREEWIDGET
    00427
    00428 QT_END_NAMESPACE
    00429
    00430 QT_END_HEADER
    00431
    00432 #endif // QTREEWIDGET_H