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

📄 qtreewidget.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    This function is useful when reimplementing clone().    \sa data(), flags()*/QTreeWidgetItem &QTreeWidgetItem::operator=(const QTreeWidgetItem &other){    values = other.values;    d->display = other.d->display;    d->policy = other.d->policy;    itemFlags = other.itemFlags;    return *this;}#endif // QT_NO_DATASTREAM/*!  Appends the \a child item to the list of children.  \sa insertChild() takeChild()*/void QTreeWidgetItem::addChild(QTreeWidgetItem *child){    insertChild(children.count(), child);}/*!  Inserts the \a child item at \a index in the list of children.  If the child has already been inserted somewhere else it wont be inserted again.*/void QTreeWidgetItem::insertChild(int index, QTreeWidgetItem *child){    if (index < 0 || index > children.count() || child == 0 || child->view != 0 || child->par != 0)        return;    if (QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0)) {        if (model->rootItem == this)            child->par = 0;        else            child->par = this;        if (view->isSortingEnabled()) {            // do a delayed sort instead            index = children.count(); // append            model->sortPending = true;        }        model->beginInsertItems(this, index, 1);        int cols = model->columnCount();        QStack<QTreeWidgetItem*> stack;        stack.push(child);        while (!stack.isEmpty()) {            QTreeWidgetItem *i = stack.pop();            i->view = view;            i->values.reserve(cols);            for (int c = 0; c < i->children.count(); ++c)                stack.push(i->children.at(c));        }        children.insert(index, child);        model->endInsertItems();    } else {        child->par = this;        children.insert(index, child);    }    if (child->par)        d->propagateDisabled(child);}/*!  Removes the given item indicated by \a child.  The removed item will not be deleted.*/void QTreeWidgetItem::removeChild(QTreeWidgetItem *child){    (void)takeChild(children.indexOf(child));}/*!  Removes the item at \a index and returns it, otherwise return 0.*/QTreeWidgetItem *QTreeWidgetItem::takeChild(int index){    // we move this outside the check of the index to allow executing    // pending sorts from inline functions, using this function (hack)    QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0);    if (model) {        // This will trigger a layoutChanged signal, thus we might want to optimize        // this function by not emitting the rowsRemoved signal etc to the view.        // On the other hand we also need to make sure that the selectionmodel        // is updated in case we take an item that is selected.        model->executePendingSort();    }    if (index >= 0 && index < children.count()) {        if (model) model->beginRemoveItems(this, index, 1);        QTreeWidgetItem *item = children.takeAt(index);        item->par = 0;        QStack<QTreeWidgetItem*> stack;        stack.push(item);        while (!stack.isEmpty()) {            QTreeWidgetItem *i = stack.pop();            i->view = 0;            for (int c = 0; c < i->children.count(); ++c)                stack.push(i->children.at(c));        }        d->propagateDisabled(item);        if (model) model->endRemoveRows();        return item;    }    return 0;}/*!  \since 4.1  Appends the given list of \a children to the item.  \sa insertChildren() takeChildren()*/void QTreeWidgetItem::addChildren(const QList<QTreeWidgetItem*> &children){    insertChildren(this->children.count(), children);}/*!  \since 4.1  Inserts the given list of \a children into the list of the item children at \a index .  Children that have already been inserted somewhere else wont be inserted.*/void QTreeWidgetItem::insertChildren(int index, const QList<QTreeWidgetItem*> &children){    if (view && view->isSortingEnabled()) {        for (int n = 0; n < children.count(); ++n)            insertChild(index, children.at(n));        return;    }    QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0);    QStack<QTreeWidgetItem*> stack;    QList<QTreeWidgetItem*> itemsToInsert;    for (int n = 0; n < children.count(); ++n) {        QTreeWidgetItem *child = children.at(n);        if (child->view || child->par)            continue;        itemsToInsert.append(child);        if (view && model) {            if (child->childCount() == 0)                child->view = view;            else                stack.push(child);        }        if (model && (model->rootItem == this))            child->par = 0;        else            child->par = this;    }    if (!itemsToInsert.isEmpty()) {        while (!stack.isEmpty()) {            QTreeWidgetItem *i = stack.pop();            i->view = view;            for (int c = 0; c < i->children.count(); ++c)                stack.push(i->children.at(c));        }        if (model) model->beginInsertItems(this, index, itemsToInsert.count());        for (int n = 0; n < itemsToInsert.count(); ++n) {            QTreeWidgetItem *child = itemsToInsert.at(n);            this->children.insert(index + n, child);            if (child->par)                d->propagateDisabled(child);        }        if (model) model->endInsertItems();    }}/*!  \since 4.1  Removes the list of children and returns it, otherwise returns an empty list.*/QList<QTreeWidgetItem*> QTreeWidgetItem::takeChildren(){    QList<QTreeWidgetItem*> removed;    if (children.count() > 0) {        QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0);        if (model) {            // This will trigger a layoutChanged signal, thus we might want to optimize            // this function by not emitting the rowsRemoved signal etc to the view.            // On the other hand we also need to make sure that the selectionmodel            // is updated in case we take an item that is selected.            model->executePendingSort();        }        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;                for (int c = 0; c < i->children.count(); ++c)                    stack.push(i->children.at(c));            }            d->propagateDisabled(item);        }        removed = children;        children.clear(); // detach        if (model) model->endRemoveItems();    }    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){    QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0);    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  Calculates the checked state of the item based on the checked state  of its children. E.g. if all children checked => this item is also  checked; if some children checked => this item is partially checked;  if no children checked => this item is unchecked.*/QVariant QTreeWidgetItem::childrenCheckState(int column) const{    if (column < 0)        return QVariant();    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;}/*!  \internal*/void QTreeWidgetItem::itemChanged(){    if (QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0))        model->itemChanged(this);}/*!  \internal*/void QTreeWidgetItem::executePendingSort() const{    if (QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0))        model->executePendingSort();}#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{    friend class QTreeModel;    Q_DECLARE_PUBLIC(QTreeWidget)public:    QTreeWidgetPrivate() : QTreeViewPrivate() {}    inline QTreeModel *model() const        { return ::qobject_cast<QTreeModel*>(q_func()->model()); }    inline QModelIndex index(const QTreeWidgetItem *item, int column = 0) const        { return model()->index(item, column); }    inline QTreeWidgetItem *item(const QModelIndex &index) const        { return model()->item(index); }    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);    void _q_sort();    void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);    void _q_itemsSorted();};void QTreeWidgetPrivate::_q_emitItemPressed(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemPressed(item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemClicked(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemClicked(item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemDoubleClicked(item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemActivated(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemActivated(item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemEntered(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemEntered(item(index), index.column());}void QTreeWidgetPrivate::_q_emitItemChanged(const QModelIndex &index){    Q_Q(QTreeWidget);    QTreeWidgetItem *indexItem = item(index);    if (indexItem)        emit q->itemChanged(indexItem, index.column());}void QTreeWidgetPrivate::_q_emitItemExpanded(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemExpanded(item(index));}void QTreeWidgetPrivate::_q_emitItemCollapsed(const QModelIndex &index){    Q_Q(QTreeWidget);    emit q->itemCollapsed(item(index));}void QTreeWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex &current,                                                const QModelIndex &previous){    Q_Q(QTreeWidget);    QTreeWidgetItem *currentItem = item(current);    QTreeWidgetItem *previousItem = item(previous);    emit q->currentItemChanged(currentItem, previousItem);}void QTreeWidgetPrivate::_q_sort(){    Q_Q(QTreeWidget);    if (sortingEnabled) {        int column = q->header()->sortIndicatorSection();        Qt::SortOrder order = q->header()->sortIndicatorOrder();        model()->sort(column, order);    }}void QTreeWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,                                        const QModelIndex &bottomRight){    Q_Q(QTreeWidget);    if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()        && !model()->sortPending) {        int column = q->header()->sortIndicatorSection();        if (column >= topLeft.column() && column <= bottomRight.column()) {            Qt::SortOrder order = q->header()->sortIndicatorOrder();            model()->ensureSorted(column, order, topLeft.row(),                                  bottomRight.row(), topLeft.parent());        }    }}void QTreeWidgetPrivate::_q_itemsSorted(){    // update the internal view items    for (int i = 0; i < viewItems.count(); ++i) {        const QModelIndex index = viewItems.at(i).index;        if (!index.isValid())            continue;        const QTreeWidgetItem *par = static_cast<QTreeWidgetItem*>(index.internalPointer())->parent();        QTreeWidgetItem *itm = par ? par->child(index.row()) : model()->rootItem->child(index.row());        viewItems[i].index = model()->createIndexFromItem(index.row(), index.column(), itm);    }}/*!  \class QTreeWidget  \brief The QTreeWidget class provides a tree view that uses a predefined  tree model.  \ingroup model-view  \mainclass  The QTreeWidget class is a

⌨️ 快捷键说明

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