📄 qtreewidget.cpp
字号:
values.reserve(model->headerItem->columnCount()); model->executePendingSort(); } }}/*! Constructs a tree widget item and append it to the given \a parent. \sa type()*/QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, int type) : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled |Qt::ItemIsDropEnabled){ if (parent) parent->addChild(this); if (view) { QTreeModel *model = ::qobject_cast<QTreeModel*>(view->model()); if (model) { model->executePendingSort(); } }}/*! Constructs a tree widget item and append it to the given \a parent. The given list of \a strings will be set as the item text for each column in the item. \sa type()*/QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type) : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled |Qt::ItemIsDropEnabled){ for (int i = 0; i < strings.count(); ++i) setText(i, strings.at(i)); if (parent) parent->addChild(this); if (view) { QTreeModel *model = ::qobject_cast<QTreeModel*>(view->model()); if (model) { model->executePendingSort(); } }}/*! \fn QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *preceding, int type) Constructs a tree widget item of the specified \a type that is inserted into the \a parent after the \a preceding child item. \sa type()*/QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type) : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled |Qt::ItemIsDropEnabled){ if (parent) { int i = parent->indexOfChild(after) + 1; parent->insertChild(i, this); } if (view) { QTreeModel *model = ::qobject_cast<QTreeModel*>(view->model()); if (model) { model->executePendingSort(); } }}/*! Destroys this tree widget item.*/QTreeWidgetItem::~QTreeWidgetItem(){ QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0); if (par) { int i = par->children.indexOf(this); if (i >= 0) { if (model) model->beginRemoveItems(par, i, 1); // users _could_ do changes when connected to rowsAboutToBeRemoved, // so we check again to make sure 'i' is valid if (!par->children.isEmpty() && par->children.at(i) == this) par->children.takeAt(i); if (model) model->endRemoveItems(); } } else if (model) { if (this == model->headerItem) { model->headerItem = 0; } else { int i = model->rootItem->children.indexOf(this); if (i >= 0) { model->beginRemoveItems(0, i, 1); // users _could_ do changes when connected to rowsAboutToBeRemoved, // so we check again to make sure 'i' is valid if (!model->rootItem->children.isEmpty() && model->rootItem->children.at(i) == this) model->rootItem->children.takeAt(i); model->endRemoveItems(); } } } // at this point the persistent indexes for the children should also be invalidated // since we invalidated the parent for (int i = 0; i < children.count(); ++i) { QTreeWidgetItem *child = children.at(i); // make sure the child does not try to remove itself from our children list child->par = 0; // make sure the child does not try to remove itself from the top level list child->view = 0; delete child; } children.clear(); delete d;}/*! Creates a deep copy of the item and of its children.*/QTreeWidgetItem *QTreeWidgetItem::clone() const{ QTreeWidgetItem *copy = 0; QStack<const QTreeWidgetItem*> stack; QStack<QTreeWidgetItem*> parentStack; stack.push(this); parentStack.push(0); QTreeWidgetItem *root = 0; const QTreeWidgetItem *item = 0; QTreeWidgetItem *parent = 0; while (!stack.isEmpty()) { // get current item, and copied parent item = stack.pop(); parent = parentStack.pop(); // copy item copy = new QTreeWidgetItem(*item); if (!root) root = copy; // set parent and add to parents children list if (parent) { copy->par = parent; parent->children.insert(0, copy); } for (int i = 0; i < item->childCount(); ++i) { stack.push(item->child(i)); parentStack.push(copy); } } return root;}/*! Sets the item indicator \a policy. This policy decides when the tree branch expand/collapse indicator is shown. The default value is ShowForChildren. \sa childIndicatorPolicy()*/void QTreeWidgetItem::setChildIndicatorPolicy(QTreeWidgetItem::ChildIndicatorPolicy policy){ if (d->policy == policy) return; d->policy = policy; if (QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0)) view->update(model->index(this, 0));}/*! Returns the item indicator policy. This policy decides when the tree branch expand/collapse indicator is shown. \sa setChildIndicatorPolicy()*/QTreeWidgetItem::ChildIndicatorPolicy QTreeWidgetItem::childIndicatorPolicy() const{ return d->policy;}/*! \fn void QTreeWidgetItem::setFlags(Qt::ItemFlags flags) Sets the flags for the item to the given \a flags. These determine whether the item can be selected or modified. This is often used to disable an item. \sa flags()*/void QTreeWidgetItem::setFlags(Qt::ItemFlags flags){ const bool enable = (flags & Qt::ItemIsEnabled); const bool changedState = bool(itemFlags & Qt::ItemIsEnabled) != enable; const bool changedExplicit = d->disabled != !enable; d->disabled = !enable; if (enable && par && !(par->itemFlags & Qt::ItemIsEnabled)) // inherit from parent itemFlags = flags & ~Qt::ItemIsEnabled; else // this item is explicitly disabled or has no parent itemFlags = flags; if (changedState && changedExplicit) { // if the propagate the change to the children QStack<QTreeWidgetItem*> parents; parents.push(this); while (!parents.isEmpty()) { QTreeWidgetItem *parent = parents.pop(); for (int i = 0; i < parent->children.count(); ++i) { QTreeWidgetItem *child = parent->children.at(i); if (!child->d->disabled) { // if not explicitly disabled parents.push(child); if (enable) child->itemFlags = child->itemFlags | Qt::ItemIsEnabled; else child->itemFlags = child->itemFlags & ~Qt::ItemIsEnabled; child->itemChanged(); // ### we may want to optimize this } } } } itemChanged();}void QTreeWidgetItemPrivate::propagateDisabled(QTreeWidgetItem *item){ Q_ASSERT(item); const bool enable = item->par ? (item->par->itemFlags.testFlag(Qt::ItemIsEnabled)) : true; QStack<QTreeWidgetItem*> parents; parents.push(item); while (!parents.isEmpty()) { QTreeWidgetItem *parent = parents.pop(); if (!parent->d->disabled) { // if not explicitly disabled if (enable) parent->itemFlags = parent->itemFlags | Qt::ItemIsEnabled; else parent->itemFlags = parent->itemFlags & ~Qt::ItemIsEnabled; parent->itemChanged(); // ### we may want to optimize this } for (int i = 0; i < parent->children.count(); ++i) { QTreeWidgetItem *child = parent->children.at(i); parents.push(child); } }}/*! \fn Qt::ItemFlags QTreeWidgetItem::flags() const Returns the flags used to describe the item. These determine whether the item can be checked, edited, and selected. The default value for flags is Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled. If the item was constructed with a parent, flags will in addition contain Qt::ItemIsDropEnabled. \sa setFlags()*/Qt::ItemFlags QTreeWidgetItem::flags() const{ return itemFlags;}/*! Sets the value for the item's \a column and \a role to the given \a value. The \a role describes the type of data specified by \a value, and is defined by the Qt::ItemDataRole enum.*/void QTreeWidgetItem::setData(int column, int role, const QVariant &value){ if (column < 0) return; QTreeModel *model = (view ? ::qobject_cast<QTreeModel*>(view->model()) : 0); switch (role) { case Qt::EditRole: case Qt::DisplayRole: { if (values.count() <= column) { if (model && this == model->headerItem) model->setColumnCount(column + 1); else values.resize(column + 1); } if (d->display.count() <= column) { for (int i = d->display.count() - 1; i < column - 1; ++i) d->display.append(QVariant()); d->display.append(value); } else if (d->display[column] != value) { d->display[column] = value; } else { return; // value is unchanged } } break; case Qt::CheckStateRole: if (itemFlags & Qt::ItemIsTristate) { for (int i = 0; i < children.count(); ++i) { QTreeWidgetItem *child = children.at(i); if (child->data(column, role).isValid()) {// has a CheckState Qt::ItemFlags f = itemFlags; // a little hack to avoid multiple dataChanged signals itemFlags &= ~Qt::ItemIsTristate; child->setData(column, role, value); itemFlags = f; } } } // Don't break, but fall through default: if (column < values.count()) { bool found = false; QVector<QWidgetItemData> column_values = values.at(column); for (int i = 0; i < column_values.count(); ++i) { if (column_values.at(i).role == role) { if (column_values.at(i).value == value) return; // value is unchanged values[column][i].value = value; found = true; break; } } if (!found) values[column].append(QWidgetItemData(role, value)); } else { if (model && this == model->headerItem) model->setColumnCount(column + 1); else values.resize(column + 1); values[column].append(QWidgetItemData(role, value)); } } if (model) { model->emitDataChanged(this, column); if (role == Qt::CheckStateRole) { QTreeWidgetItem *p; for (p = par; p && (p->itemFlags & Qt::ItemIsTristate); p = p->par) model->emitDataChanged(p, column); } }}/*! Returns the value for the item's \a column and \a role.*/QVariant QTreeWidgetItem::data(int column, int role) const{ switch (role) { case Qt::EditRole: case Qt::DisplayRole: if (column >= 0 && column < d->display.count()) return d->display.at(column); break; case Qt::CheckStateRole: // special case for check state in tristate if (children.count() && (itemFlags & Qt::ItemIsTristate)) return childrenCheckState(column); default: if (column >= 0 && column < values.size()) { const QVector<QWidgetItemData> &column_values = values.at(column); for (int i = 0; i < column_values.count(); ++i) if (column_values.at(i).role == role) return column_values.at(i).value; } } return QVariant();}/*! Returns true if the text in the item is less than the text in the \a other item, otherwise returns false.*/bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const{ int column = view ? view->sortColumn() : 0; return text(column) < other.text(column);}#ifndef QT_NO_DATASTREAM/*! Reads the item from stream \a in. This only reads data into a single item. \sa write()*/void QTreeWidgetItem::read(QDataStream &in){ // convert from streams written before we introduced display (4.2.0) if (in.version() < QDataStream::Qt_4_2) { d->display.clear(); in >> values; // move the display value over to the display string list for (int column = 0; column < values.count(); ++column) { d->display << QVariant(); for (int i = 0; i < values.at(column).count(); ++i) { if (values.at(column).at(i).role == Qt::DisplayRole) { d->display[column] = values.at(column).at(i).value; values[column].remove(i--); } } } } else { in >> values >> d->display; }}/*! Writes the item to stream \a out. This only writes data from one single item. \sa read()*/void QTreeWidgetItem::write(QDataStream &out) const{ out << values << d->display;}/*! \since 4.1 Constructs a copy of \a other. Note that type() and treeWidget() are not copied. This function is useful when reimplementing clone(). \sa data(), flags()*/QTreeWidgetItem::QTreeWidgetItem(const QTreeWidgetItem &other) : rtti(Type), values(other.values), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), itemFlags(other.itemFlags){ d->display = other.d->display;}/*! Assigns \a other's data and flags to this item. Note that type() and treeWidget() are not copied.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -