⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtreewidget.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        if (model) model->beginRemoveItems(this, 0, children.count());        for (int n = 0; n < children.count(); ++n) {            QTreeWidgetItem *item = children.at(n);            item->par = 0;            QStack<QTreeWidgetItem*> stack;            stack.push(item);            while (!stack.isEmpty()) {                QTreeWidgetItem *i = stack.pop();                i->view = 0;                i->model = 0;                for (int c = 0; c < i->children.count(); ++c)                    stack.push(i->children.at(c));            }        }        removed = children;        children.clear(); // detach        if (model) model->endRemoveRows();    }    return removed;}/*!  \internal  Sorts the children by the value in the given \a column, in the \a order  specified. If \a climb is true, the items below each of the children will  also be sorted.*/void QTreeWidgetItem::sortChildren(int column, Qt::SortOrder order, bool climb){    if (!model)        return;    model->sortItems(&children, column, order);    if (climb) {        QList<QTreeWidgetItem*>::iterator it = children.begin();        for (; it != children.end(); ++it)            (*it)->sortChildren(column, order, climb);    }}/*!  \internal*/QVariant QTreeWidgetItem::childrenCheckState(int column) const{    int checkedChildrenCount = 0;    int uncheckedChildrenCount = 0;    int validChildrenCount = 0;    for (int i = 0; i < children.count(); ++i) {        QVariant value = children.at(i)->data(column, Qt::CheckStateRole);        if (!value.isValid())            continue;        Qt::CheckState checkState = static_cast<Qt::CheckState>(value.toInt());        if (checkState == Qt::Unchecked)            ++uncheckedChildrenCount;        else            ++checkedChildrenCount; // includes partially checked items        ++validChildrenCount;    }    if (checkedChildrenCount + uncheckedChildrenCount == 0)        return QVariant(); // value was not defined    if (checkedChildrenCount == validChildrenCount)        return Qt::Checked;    if (uncheckedChildrenCount == validChildrenCount)        return Qt::Unchecked;    return Qt::PartiallyChecked;}#ifndef QT_NO_DATASTREAM/*!    \relates QTreeWidgetItem    Writes the tree widget item \a item to stream \a out.    This operator uses QTreeWidgetItem::write().    \sa {Format of the QDataStream Operators}*/QDataStream &operator<<(QDataStream &out, const QTreeWidgetItem &item){    item.write(out);    return out;}/*!    \relates QTreeWidgetItem    Reads a tree widget item from stream \a in into \a item.    This operator uses QTreeWidgetItem::read().    \sa {Format of the QDataStream Operators}*/QDataStream &operator>>(QDataStream &in, QTreeWidgetItem &item){    item.read(in);    return in;}#endif // QT_NO_DATASTREAMclass QTreeWidgetPrivate : public QTreeViewPrivate{    Q_DECLARE_PUBLIC(QTreeWidget)public:    QTreeWidgetPrivate() : QTreeViewPrivate(), sortingEnabled(false) {}    inline QTreeModel *model() const { return ::qobject_cast<QTreeModel*>(q_func()->model()); }    void _q_emitItemPressed(const QModelIndex &index);    void _q_emitItemClicked(const QModelIndex &index);    void _q_emitItemDoubleClicked(const QModelIndex &index);    void _q_emitItemActivated(const QModelIndex &index);    void _q_emitItemEntered(const QModelIndex &index);    void _q_emitItemChanged(const QModelIndex &index);    void _q_emitItemExpanded(const QModelIndex &index);    void _q_emitItemCollapsed(const QModelIndex &index);    void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &index);    bool sortingEnabled;};void QTreeWidgetPrivate::_q_emitItemPressed(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemPressed(model()->item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemClicked(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemClicked(model()->item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemDoubleClicked(model()->item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemActivated(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemActivated(model()->item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemEntered(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemEntered(model()->item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemChanged(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemChanged(model()->item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemExpanded(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemExpanded(model()->item(index));}void QTreeWidgetPrivate::_q_emitItemCollapsed(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemCollapsed(model()->item(index));}void QTreeWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex &current,                                                const QModelIndex &previous){    Q_Q(QTreeWidget);    QTreeWidgetItem *currentItem = model()->item(current);    QTreeWidgetItem *previousItem = model()->item(previous);    emit q->currentItemChanged(currentItem, previousItem);}/*!  \class QTreeWidget qtreewidget.h  \brief The QTreeWidget class provides a tree view that uses a predefined  tree model.  \ingroup model-view  \mainclass  The QTreeWidget class is a convenience class that provides a standard  tree widget with a classic item-based interface similar to that used by  the QListView class in Qt 3.  This class is based on Qt's Model/View architecture and uses a default  model to hold items, each of which is a QTreeWidgetItem.  Developers who do not need the flexibility of the Model/View framework  can use this class to create simple hierarchical lists very easily. A more  flexible approach involves combining a QTreeView with a standard item model.  This allows the storage of data to be separated from its representation.  In its simplest form, a tree widget can be constructed in the following way:  \code    QTreeWidget *treeWidget = new QTreeWidget();    treeWidget->setColumnCount(1);    QList<QTreeWidgetItem *> items;    for (int i = 0; i < 10; ++i)        items.append(new QTreeWidgetItem(0, QStringList(QString("item: %1").arg(i))));    treeWidget->insertTopLevelItems(0, items);  \endcode  Before items can be added to the tree widget, the number of columns must  be set with setColumnCount(). This allows each item to have one or more  labels or other decorations. The number of columns in use can be found  with the columnCount() function.  The tree can have a header that contains a section for each column in  the widget. It is easiest to set up the labels for each section by  supplying a list of strings with setHeaderLabels(), but a custom header  can be constructed with a QTreeWidgetItem and inserted into the tree  with the setHeaderItem() function.  The items in the tree can be sorted by column according to a predefined  sort order. If sorting is enabled, the user can sort the items by clicking  on a column header. Sorting can be enabled or disabled by calling  setSortingEnabled(). The isSortingEnabled() function indicates whether  sorting is enabled.  \table 100%  \row \o \inlineimage windowsxp-treeview.png Screenshot of a Windows XP style tree widget       \o \inlineimage macintosh-treeview.png Screenshot of a Macintosh style tree widget       \o \inlineimage plastique-treeview.png Screenshot of a Plastique style tree widget  \row \o A \l{Windows XP Style Widget Gallery}{Windows XP style} tree widget.       \o A \l{Macintosh Style Widget Gallery}{Macintosh style} tree widget.       \o A \l{Plastique Style Widget Gallery}{Plastique style} tree widget.  \endtable  \sa QTreeWidgetItem, QTreeView, {Model/View Programming}*//*!    \property QTreeWidget::columnCount    \brief the number of columns displayed in the tree widget*//*!    \property QTreeWidget::sortingEnabled    \brief whether the items in the tree widget can be sorted    by clicking on the header*//*!    \fn void QTreeWidget::itemActivated(QTreeWidgetItem *item, int column)    This signal is emitted when the user activates an item by single-    or double-clicking (depending on the platform, i.e. on the    QStyle::SH_ItemView_ActivateItemOnSingleClick style hint) or    pressing a special key (e.g., \key Enter).    The specified \a item is the item that was clicked, or 0 if no    item was clicked. The \a column is the item's column that was    clicked, or -1 if no item was clicked.*//*!    \fn void QTreeWidget::itemPressed(QTreeWidgetItem *item, int column)    This signal is emitted when the user presses a mouse button inside    the widget. The specified \a item is the item that was clicked.*//*!    \fn void QTreeWidget::itemClicked(QTreeWidgetItem *item, int column)    This signal is emitted when the user clicks inside the widget.    The specified \a item is the item that was clicked.*//*!    \fn void QTreeWidget::itemDoubleClicked(QTreeWidgetItem *item, int column)    This signal is emitted when the user double clicks inside the    widget.  The specified \a item is the item that was clicked.*//*!    \fn void QTreeWidget::itemExpanded(QTreeWidgetItem *item)    This signal is emitted when the specified \a item is expanded so that    all of its children are displayed.    \sa isItemExpanded()*//*!    \fn void QTreeWidget::itemCollapsed(QTreeWidgetItem *item)    This signal is emitted when the specified \a item is collapsed so that    none of its children are displayed.    \sa isItemExpanded()*//*!    \fn void QTreeWidget::currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)    This signal is emitted when the current item changes. The current    item is specified by \a current, and this replaces the \a previous    current item.    \sa setCurrentItem()*//*!    \fn void QTreeWidget::itemSelectionChanged()    This signal is emitted when the selection changes in the tree widget.    The current selection can be found with selectedItems().*//*!    \fn void QTreeWidget::itemEntered(QTreeWidgetItem *item, int column)    This signal is emitted when the mouse cursor enters an \a item over the    specified \a column.    QTreeWidget mouse tracking needs to be enabled for this feature to work.*//*!    \fn void QTreeWidget::itemChanged(QTreeWidgetItem *item, int column)    This signal is emitted when the contents of the \a column in the specified    \a item changes.*//*!  Constructs a tree widget with the given \a parent.*/QTreeWidget::QTreeWidget(QWidget *parent)    : QTreeView(*new QTreeWidgetPrivate(), parent){    QTreeView::setModel(new QTreeModel(0, this));    // view signals    connect(this, SIGNAL(pressed(QModelIndex)), SLOT(_q_emitItemPressed(QModelIndex)));    connect(this, SIGNAL(clicked(QModelIndex)), SLOT(_q_emitItemClicked(QModelIndex)));    connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(_q_emitItemDoubleClicked(QModelIndex)));    connect(this, SIGNAL(activated(QModelIndex)), SLOT(_q_emitItemActivated(QModelIndex)));    connect(this, SIGNAL(entered(QModelIndex)), SLOT(_q_emitItemEntered(QModelIndex)));    connect(this, SIGNAL(expanded(QModelIndex)), SLOT(_q_emitItemExpanded(QModelIndex)));    connect(this, SIGNAL(collapsed(QModelIndex)), SLOT(_q_emitItemCollapsed(QModelIndex)));    // selection signals    connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),            this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));    connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),            this, SIGNAL(itemSelectionChanged()));    // model signals    connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),            this, SLOT(_q_emitItemChanged(QModelIndex)));    header()->setClickable(false);}/*!    Destroys the tree widget and all its items.*/QTreeWidget::~QTreeWidget(){}/*  Retuns the number of header columns in the view.  \sa sortColumn(), currentColumn(), topLevelItemCount()*/int QTreeWidget::columnCount() const{    Q_D(const QTreeWidget);    return d->model()->columnCount();}/*  Sets the number of header \a columns in the tree widget.*/void QTreeWidget::setColumnCount(int columns){    Q_D(QTreeWidget);    if (columns < 0)        return;    d->model()->setColumnCount(columns);}/*!  Returns the top level item at the given \a index, or 0 if the item does  not exist.  \sa topLevelItemCount(), insertTopLevelItem()*/QTreeWidgetItem *QTreeWidget::topLevelItem(int index) const{    Q_D(const QTreeWidget);    return d->model()->tree.value(index);}/*!\property QTreeWidget::topLevelItemCount    \brief the number of top-level items    \sa columnCount(), currentItem()*/int QTreeWidget::topLevelItemCount() const{    Q_D(const QTreeWidget);    return d->model()->tree.count();}/*!  Inserts the \a item at \a index in the top level in the view.  \sa addTopLevelItem(), columnCount()*/void QTreeWidget::insertTopLevelItem(int index, QTreeWidgetItem *item){    Q_D(QTreeWidget);    if (index < 0 || index > d->model()->tree.count() || item == 0)        return;    Q_ASSERT(!item->view || !item->model || !item->par);    QStack<QTreeWidgetItem*> stack;    stack.push(item);    while (!stack.isEmpty()) {        QTreeWidgetItem *i = stack.pop();        i->view = this;        i->model = d->model();        for (int c = 0; c < i->children.count(); ++c)            stack.push(i->children.at(c));    }    d->model()->insertInTopLevel(index, item);}/*!    \since 4.1    Appends the \a item as a top-level item in the widget.    \sa insertTopLevelItem()*/void QTreeWidget::addTopLevelItem(QTreeWidgetItem *item){    insertTopLevelItem(topLevelItemCount(), item);}/*!  Removes the top-level item at the given \a index in the tree and  returns it, otherwise returns 0;  \sa insertTopLevelItem(), topLevelItem(), topLevelItemCount()*/QTreeWidgetItem *QTreeWidget::takeTopLevelItem(int index){    Q_D(QTreeWidget);    if (index >= 0 && index < d->model()->tree.count()) {        d->model()->beginRemoveRows(QModelIndex(), index, index);        QTreeWidgetItem *item = d->model()->tree.takeAt(index);        QStack<QTreeWidgetItem*> stack;        stack.push(item);        while (!stack.isEmpty()) {            QTreeWidgetItem *i = stack.pop();            i->view = 0;            i->model = 0;            for (int c = 0; c < i->children.count(); ++c)                stack.push(i->children.at(c));        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -