📄 qtreewidget.cpp
字号:
|Qt::ItemIsDropEnabled){}/*! Constructs a tree widget item of the specified \a type. The item must be inserted into a tree widget. The given list of \a strings will be set as the item text for each column in the item. \sa type()*/QTreeWidgetItem::QTreeWidgetItem(const QStringList &strings, int type) : rtti(type), view(0), model(0), 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));}/*! \fn QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *parent, int type) Constructs a tree widget item of the specified \a type and appends it to the items in the given \a parent. \sa type()*/QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, int type) : rtti(type), view(view), model(0), par(0), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled |Qt::ItemIsDropEnabled){ if (view && view->model()) { model = ::qobject_cast<QTreeModel*>(view->model()); model->insertInTopLevel(model->rowCount(QModelIndex()), this); }}/*! \fn QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *parent, const QStringList &strings, int type) Constructs a tree widget item of the specified \a type and appends it to the items in 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(QTreeWidget *view, const QStringList &strings, int type) : rtti(type), view(view), model(0), 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 (view && view->model()) { model = ::qobject_cast<QTreeModel*>(view->model()); model->insertInTopLevel(model->rowCount(QModelIndex()), this); }}/*! \fn QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *parent, QTreeWidgetItem *preceding, int type) Constructs a tree widget item of the specified \a type and inserts it into the given \a parent after the \a preceding item. \sa type()*/QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, QTreeWidgetItem *after, int type) : rtti(type), view(view), model(0), par(0), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled |Qt::ItemIsDropEnabled){ if (view) { model = ::qobject_cast<QTreeModel*>(view->model()); if (model) { int i = model->tree.indexOf(after) + 1; model->insertInTopLevel(i, this); } }}/*! 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), model(0), par(parent), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled |Qt::ItemIsDropEnabled){ if (parent) parent->addChild(this);}/*! 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), model(0), par(parent), 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);}/*! \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), model(0), par(parent), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled |Qt::ItemIsDropEnabled){ if (parent) { int i = parent->indexOfChild(after) + 1; parent->insertChild(i, this); }}/*! Destroys this tree widget item.*/QTreeWidgetItem::~QTreeWidgetItem(){ if (par) { int i = par->children.indexOf(this); if (model) model->beginRemoveItems(par, i, 1); par->children.takeAt(i); if (model) model->endRemoveRows(); } else if (model) { if (this == model->header) { model->header = 0; } else { int i = model->tree.indexOf(this); model->beginRemoveItems(0, i, 1); model->tree.takeAt(i); model->endRemoveRows(); } } // 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); child->par = 0; // make sure the child doesn't try to remove itself from children list child->view = 0; // make sure the child doesn't try to remove itself from the top level list child->model = 0; // make sure the child doesn't try to invalidate its index in the model delete child; } children.clear();}/*! 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.append(copy); } for (int i=0; i<item->childCount(); ++i) { stack.push(item->child(i)); parentStack.push(copy); } } return root;}/*! 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){ // special case for check state in tristate if ((role == Qt::CheckStateRole) && (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; } } } // set the item data role = (role == Qt::EditRole ? Qt::DisplayRole : role); if (column >= values.count()) values.resize(column + 1); 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) { values[column][i].value = value; found = true; break; } } if (!found) values[column].append(QWidgetItemData(role, value)); if (model) { model->emitDataChanged(this, column); if (role == Qt::CheckStateRole && par && par->itemFlags & Qt::ItemIsTristate) model->emitDataChanged(par, column); }}/*! Returns the value for the item's \a column and \a role.*/QVariant QTreeWidgetItem::data(int column, int role) const{ // special case for check state in tristate if (role == Qt::CheckStateRole && (itemFlags & Qt::ItemIsTristate) && children.count()) { return childrenCheckState(column); } // return the item data role = (role == Qt::EditRole ? Qt::DisplayRole : role); 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->sortColumn(); 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){ in >> values;}/*! 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;}/*! \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), model(0), par(0), itemFlags(other.itemFlags){}/*! Assigns \a other's data and flags to this item. Note that type() and treeWidget() are not copied. This function is useful when reimplementing clone(). \sa data(), flags()*/QTreeWidgetItem &QTreeWidgetItem::operator=(const QTreeWidgetItem &other){ values = other.values; 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.*/void QTreeWidgetItem::insertChild(int index, QTreeWidgetItem *child){ // FIXME: here we have a problem; // the user could build up a tree and then insert the root in the view if (index < 0 || index > children.count() || child == 0) return; Q_ASSERT(!child->view || !child->model || !child->par); if (model) model->beginInsertItems(this, index, 1); if (view && model) { QStack<QTreeWidgetItem*> stack; stack.push(child); child->par = this; while (!stack.isEmpty()) { QTreeWidgetItem *i = stack.pop(); i->view = view; i->model = model; for (int c = 0; c < i->children.count(); ++c) stack.push(i->children.at(c)); } } children.insert(index, child); if (model) model->endInsertRows();}/*! Removes the item at \a index and returns it, otherwise return 0.*/QTreeWidgetItem *QTreeWidgetItem::takeChild(int index){ 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; i->model = 0; for (int c = 0; c < i->children.count(); ++c) stack.push(i->children.at(c)); } 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 .*/void QTreeWidgetItem::insertChildren(int index, const QList<QTreeWidgetItem*> &children){ // FIXME: here we have a problem; // the user could build up a tree and then insert the root in the view if (model) model->beginInsertItems(this, index, children.count()); for (int n = 0; n < children.count(); ++n) { QTreeWidgetItem *child = children.at(n); Q_ASSERT(!child->view || !child->model || !child->par); if (view && model) { QStack<QTreeWidgetItem*> stack; stack.push(child); children.at(n)->par = this; while (!stack.isEmpty()) { QTreeWidgetItem *i = stack.pop(); i->view = view; i->model = model; for (int c = 0; c < i->children.count(); ++c) stack.push(i->children.at(c)); } } this->children.insert(index + n, child); } if (model) model->endInsertRows();}/*! \since 4.1 Removes the list of children and returns it, otherwise return an empty list.*/QList<QTreeWidgetItem*> QTreeWidgetItem::takeChildren(){ QList<QTreeWidgetItem*> removed; if (children.count() > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -