📄 qtreewidget.cpp
字号:
}/*! \internal Inserts the tree view \a item to the tree model as a toplevel item.*/void QTreeModel::insertInTopLevel(int row, QTreeWidgetItem *item){ beginInsertRows(QModelIndex(), row, row); tree.insert(row, item); endInsertRows();}/*! \internal Inserts the list of tree view \a items to the tree model as a toplevel item.*/void QTreeModel::insertListInTopLevel(int row, const QList<QTreeWidgetItem*> &items){ beginInsertRows(QModelIndex(), row, row + items.count() - 1); for (int n = 0; n < items.count(); ++n) tree.insert(row, items.at(n)); endInsertRows();}QStringList QTreeModel::mimeTypes() const{ const QTreeWidget *view = ::qobject_cast<const QTreeWidget*>(QObject::parent()); return view->mimeTypes();}QMimeData *QTreeModel::internalMimeData() const{ return QAbstractItemModel::mimeData(cachedIndexes);}QMimeData *QTreeModel::mimeData(const QModelIndexList &indexes) const{ QList<QTreeWidgetItem*> items; for (int i = 0; i < indexes.count(); ++i) if (indexes.at(i).column() == 0) // only one item per row items << item(indexes.at(i)); const QTreeWidget *view = ::qobject_cast<const QTreeWidget*>(QObject::parent()); // cachedIndexes is a little hack to avoid copying from QModelIndexList to QList<QTreeWidgetItem*> and back again in the view cachedIndexes = indexes; QMimeData *mimeData = view->mimeData(items); cachedIndexes.clear(); return mimeData;}bool QTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent){ QTreeWidget *view = ::qobject_cast<QTreeWidget*>(QObject::parent()); if (row == -1 && column == -1) row = rowCount(parent); // append return view->dropMimeData(item(parent), row, data, action);}Qt::DropActions QTreeModel::supportedDropActions() const{ const QTreeWidget *view = ::qobject_cast<const QTreeWidget*>(QObject::parent()); return view->supportedDropActions();}/*! \internal Emits the dataChanged() signal for the given \a item.*/void QTreeModel::emitDataChanged(QTreeWidgetItem *item, int column){ if (item->model->header == item) { emit headerDataChanged(Qt::Horizontal, column, column); return; } QModelIndex br, tl; if (column == -1) { // the whole row tl = index(item, 0); br = createIndex(tl.row(), columnCount() - 1, item); } else { // single cell tl = index(item, column); br = tl; } emit dataChanged(tl, br);}void QTreeModel::beginInsertItems(QTreeWidgetItem *parent, int row, int count){ beginInsertRows(index(parent, 0), row, row + count - 1);}void QTreeModel::beginRemoveItems(QTreeWidgetItem *parent, int row, int count){ beginRemoveRows(index(parent, 0), row, row + count - 1); // now update the iterators for (int i = 0; i < iterators.count(); ++i) { for (int j = 0; j < count; j++) { QTreeWidgetItem *c; if (parent) { c = parent->child(row + j); } else { c = tree.at(row + j); } iterators[i]->d_func()->ensureValidIterator(c); } }}void QTreeModel::sortItems(QList<QTreeWidgetItem*> *items, int /*column*/, Qt::SortOrder order){ // store the original order of indexes QVector< QPair<QTreeWidgetItem*,int> > sorting(items->count()); for (int i = 0; i < sorting.count(); ++i) { sorting[i].first = items->at(i); sorting[i].second = i; } // do the sorting LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan); qSort(sorting.begin(), sorting.end(), compare); int colCount = header->columnCount(); for (int r = 0; r < sorting.count(); ++r) { QTreeWidgetItem *item = sorting.at(r).first; items->replace(r, item); int oldRow = sorting.at(r).second; for (int c = 0; c < colCount; ++c) { QModelIndex from = createIndex(oldRow, c, item); QModelIndex to = createIndex(r, c, item); changePersistentIndex(from, to); } }}/*! \class QTreeWidgetItem \brief The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class. \ingroup model-view Tree widget items are used to hold rows of information for tree widgets. Rows usually contain several columns of data, each of which can contain a text label and an icon. The QTreeWidgetItem class is a convenience class that replaces the QListViewItem class in Qt 3. It provides an item for use with the QTreeWidget class. Items are usually constructed with a parent that is either a QTreeWidget (for top-level items) or a QTreeWidgetItem (for items on lower levels of the tree). For example, the following code constructs a top-level item to represent cities of the world, and adds a entry for Oslo as a child item: \quotefile snippets/qtreewidget-using/mainwindow.cpp \skipto QTreeWidgetItem *cities \printuntil osloItem->setText(1, tr("Yes")); Items can be added in a particular order by specifying the item they follow when they are constructed: \skipto QTreeWidgetItem *planets \printuntil planets->setText(0 Each column in an item can have its own background color which is set with the setBackgroundColor() function. The current background color can be found with backgroundColor(). The text label for each column can be rendered with its own font and text color. These are specified with the setFont() and setTextColor() functions, and read with font() and textColor(). The main difference between top-level items and those in lower levels of the tree is that a top-level item has no parent(). This information can be used to tell the difference between items, and is useful to know when inserting and removing items from the tree. Children of an item can be removed with takeChild() and inserted at a given index in the list of children with the insertChild() function. By default, items are enabled, selectable, checkable, and can be the source of a drag and drop operation. Each item's flags can be changed by calling setFlags() with the appropriate value (see \l{Qt::ItemFlags}). Checkable items can be checked and unchecked with the setChecked() function. The corresponding checked() function indicates whether the item is currently checked. \sa QTreeWidget, {Model/View Programming}, QListWidgetItem, QTableWidgetItem*//*! \variable QTreeWidgetItem::Type The default type for tree widget items. \sa UserType, type()*//*! \variable QTreeWidgetItem::UserType The minimum value for custom types. Values below UserType are reserved by Qt. \sa Type, type()*//*! \fn int QTreeWidgetItem::type() const Returns the type passed to the QTreeWidgetItem constructor.*//*! \fn QTreeWidget *QTreeWidgetItem::treeWidget() const Returns the tree widget that contains the item.*//*! \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()*//*! \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. \sa flags()*//*! \fn QString QTreeWidgetItem::text(int column) const Returns the text in the specified \a column. \sa setText()*//*! \fn void QTreeWidgetItem::setText(int column, const QString &text) Sets the text to be displayed in the given \a column to the given \a text. \sa text() setFont() setTextColor()*//*! \fn QIcon QTreeWidgetItem::icon(int column) const Returns the icon that is displayed in the specified \a column. \sa setIcon(), {QAbstractItemView::iconSize}{iconSize}*//*! \fn void QTreeWidgetItem::setIcon(int column, const QIcon &icon) Sets the icon to be displayed in the given \a column to \a icon. \sa icon(), setText(), {QAbstractItemView::iconSize}{iconSize}*//*! \fn QString QTreeWidgetItem::statusTip(int column) const Returns the status tip for the contents of the given \a column. \sa setStatusTip()*//*! \fn void QTreeWidgetItem::setStatusTip(int column, const QString &statusTip) Sets the status tip for the given \a column to the given \a statusTip. QTreeWidget mouse tracking needs to be enabled for this feature to work. \sa statusTip() setToolTip() setWhatsThis()*//*! \fn QString QTreeWidgetItem::toolTip(int column) const Returns the tool tip for the given \a column. \sa setToolTip()*//*! \fn void QTreeWidgetItem::setToolTip(int column, const QString &toolTip) Sets the tooltip for the given \a column to \a toolTip. \sa toolTip() setStatusTip() setWhatsThis()*//*! \fn QString QTreeWidgetItem::whatsThis(int column) const Returns the "What's This?" help for the contents of the given \a column. \sa setWhatsThis()*//*! \fn void QTreeWidgetItem::setWhatsThis(int column, const QString &whatsThis) Sets the "What's This?" help for the given \a column to \a whatsThis. \sa whatsThis() setStatusTip() setToolTip()*//*! \fn QFont QTreeWidgetItem::font(int column) const Returns the font used to render the text in the specified \a column. \sa setFont()*//*! \fn void QTreeWidgetItem::setFont(int column, const QFont &font) Sets the font used to display the text in the given \a column to the given \a font. \sa font() setText() setTextColor()*//*! \fn QColor QTreeWidgetItem::backgroundColor(int column) const Returns the color used to render the background of the specified \a column. \sa textColor() setBackgroundColor()*//*! \fn void QTreeWidgetItem::setBackgroundColor(int column, const QColor &color) Sets the background color of the label in the given \a column to the specified \a color. \sa backgroundColor() setTextColor()*//*! \fn QColor QTreeWidgetItem::textColor(int column) const Returns the color used to render the text in the specified \a column. \sa backgroundColor() setTextColor()*//*! \fn void QTreeWidgetItem::setTextColor(int column, const QColor &color) Sets the color used to display the text in the given \a column to \a color. \sa textColor() setFont() setText()*//*! \fn Qt::CheckState QTreeWidgetItem::checkState(int column) const Returns the check state of the label in the given \a column. \sa Qt::CheckState*//*! \fn void QTreeWidgetItem::setCheckState(int column, Qt::CheckState state) Sets the item in the given \a column check state to be \a state. \sa checkState()*//*! \fn QSize QTreeWidgetItem::sizeHint(int column) const \since 4.1 Returns the size hint set for the tree item in the given \a column (see \l{QSize}).*//*! \fn void QTreeWidgetItem::setSizeHint(int column, const QSize &size) \since 4.1 Sets the size hint for the tree item in the given \a column to be \a size. If no size hint is set, the item delegate will compute the size hint based on the item data.*//*! \fn QTreeWidgetItem *QTreeWidgetItem::parent() const Returns the item's parent. \sa child()*//*! \fn QTreeWidgetItem *QTreeWidgetItem::child(int index) const Returns the item at the given \a index in the list of the item's children. \sa parent()*//*! \fn int QTreeWidgetItem::childCount() const Returns the number of child items.*//*! \fn int QTreeWidgetItem::columnCount() const Returns the number of columns in the item.*//*! \fn int QTreeWidgetItem::textAlignment(int column) const Returns the text alignment for the label in the given \a column (see \l{Qt::AlignmentFlag}).*//*! \fn void QTreeWidgetItem::setTextAlignment(int column, int alignment) Sets the text alignment for the label in the given \a column to the \a alignment specified (see \l{Qt::AlignmentFlag}).*//*! \fn int QTreeWidgetItem::indexOfChild(QTreeWidgetItem *child) const Returns the index of the given \a child in the item's list of children.*//*! Constructs a tree widget item of the specified \a type. The item must be inserted into a tree widget. \sa type()*/QTreeWidgetItem::QTreeWidgetItem(int type) : rtti(type), view(0), model(0), par(0), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -