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

📄 qtreewidget.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    endInsertColumns();    return true;}/*!  \internal  \reimp*/bool QTreeModel::removeRows(int row, int count, const QModelIndex &parent) {    if (count < 1 || row < 0 || (row + count) > rowCount(parent))        return false;    beginRemoveRows(parent, row, row + count - 1);    bool blockSignal = signalsBlocked();    blockSignals(true);    QTreeWidgetItem *itm = item(parent);    for (int i = row + count - 1; i >= row; --i) {        QTreeWidgetItem *child = itm ? itm->takeChild(i) : rootItem->children.takeAt(i);        Q_ASSERT(child);        child->view = 0;        delete child;        child = 0;    }    blockSignals(blockSignal);    endRemoveRows();    return true;}/*!  \internal  \reimp  Returns the header data corresponding to the given header \a section,  \a orientation and data \a role.*/QVariant QTreeModel::headerData(int section, Qt::Orientation orientation, int role) const{    if (orientation != Qt::Horizontal)        return QVariant();    if (headerItem)        return headerItem->data(section, role);    if (role == Qt::DisplayRole)        return QString::number(section + 1);    return QVariant();}/*!  \internal  \reimp  Sets the header data for the item specified by the header \a section,  \a orientation and data \a role to the given \a value.  Returns true if successful; otherwise returns false.*/bool QTreeModel::setHeaderData(int section, Qt::Orientation orientation,                               const QVariant &value, int role){    if (section < 0 || orientation != Qt::Horizontal || !headerItem)        return false;    headerItem->setData(section, role, value);    return true;}/*!  \reimp  Returns the flags for the item referred to the given \a index.*/Qt::ItemFlags QTreeModel::flags(const QModelIndex &index) const{    if (!index.isValid())        return Qt::ItemIsDropEnabled; // can drop on the viewport    QTreeWidgetItem *itm = item(index);    Q_ASSERT(itm);    return itm->flags();}/*!  \internal  Sorts the entire tree in the model in the given \a order,  by the values in the given \a column.*/void QTreeModel::sort(int column, Qt::SortOrder order){    sortPending = false;    if (column < 0 || column >= columnCount())        return;    emit layoutAboutToBeChanged();    rootItem->sortChildren(column, order, true);    emit layoutChanged();}/*!  \internal*/void QTreeModel::ensureSorted(int column, Qt::SortOrder order,                              int start, int end, const QModelIndex &parent){    sortPending = false;    if (column < 0 || column >= columnCount())        return;    QTreeWidgetItem *itm = item(parent);    if (!itm)        itm = rootItem;    QList<QTreeWidgetItem*> lst = itm->children;    int count = end - start + 1;    QVector < QPair<QTreeWidgetItem*,int> > sorting(count);    for (int i = 0; i < count; ++i) {        sorting[i].first = lst.at(start + i);        sorting[i].second = start + i;    }    LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);    qStableSort(sorting.begin(), sorting.end(), compare);    QModelIndexList oldPersistentIndexes;    QModelIndexList newPersistentIndexes;    QList<QTreeWidgetItem*>::iterator lit = lst.begin();    bool changed = false;    for (int i = 0; i < count; ++i) {        int oldRow = sorting.at(i).second;        QTreeWidgetItem *item = lst.takeAt(oldRow);        lit = sortedInsertionIterator(lit, lst.end(), order, item);        int newRow = qMax(lit - lst.begin(), 0);        lit = lst.insert(lit, item);        if (newRow != oldRow) {            // we are going to change the persistent indexes, so we need to prepare            if (!changed) { // this will only happen once                changed = true;                emit layoutAboutToBeChanged(); // the selection model needs to know                oldPersistentIndexes = persistentIndexList();                newPersistentIndexes = oldPersistentIndexes;            }            for (int j = i + 1; j < count; ++j) {                int otherRow = sorting.at(j).second;                if (oldRow < otherRow && newRow >= otherRow)                    --sorting[j].second;                else if (oldRow > otherRow && newRow <= otherRow)                    ++sorting[j].second;            }            for (int k = 0; k < newPersistentIndexes.count(); ++k) {                QModelIndex pi = newPersistentIndexes.at(k);                if (pi.parent() != parent)                    continue;                int oldPersistentRow = pi.row();                int newPersistentRow = oldPersistentRow;                if (oldPersistentRow == oldRow)                    newPersistentRow = newRow;                else if (oldRow < oldPersistentRow && newRow >= oldPersistentRow)                    newPersistentRow = oldPersistentRow - 1;                else if (oldRow > oldPersistentRow && newRow <= oldPersistentRow)                    newPersistentRow = oldPersistentRow + 1;                if (newPersistentRow != oldPersistentRow)                    newPersistentIndexes[k] = createIndex(newPersistentRow,                                                          pi.column(), pi.internalPointer());            }        }    }    if (changed) {        itm->children = lst;        changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes);        emit layoutChanged();    }}/*!  \internal  Returns true if the value of the \a left item is  less than the value of the \a right item.  Used by the sorting functions.*/bool QTreeModel::itemLessThan(const QPair<QTreeWidgetItem*,int> &left,                              const QPair<QTreeWidgetItem*,int> &right){    return *(left.first) < *(right.first);}/*!  \internal  Returns true if the value of the \a left item is  greater than the value of the \a right item.  Used by the sorting functions.*/bool QTreeModel::itemGreaterThan(const QPair<QTreeWidgetItem*,int> &left,                                 const QPair<QTreeWidgetItem*,int> &right){    return !(*(left .first) < *(right.first));}/*!  \internal*/QList<QTreeWidgetItem*>::iterator QTreeModel::sortedInsertionIterator(    const QList<QTreeWidgetItem*>::iterator &begin,    const QList<QTreeWidgetItem*>::iterator &end,    Qt::SortOrder order, QTreeWidgetItem *item){    if (order == Qt::AscendingOrder)        return qLowerBound(begin, end, item, QTreeModelLessThan());    return qLowerBound(begin, end, item, QTreeModelGreaterThan());}QStringList QTreeModel::mimeTypes() const{    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));    }    // 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){    if (row == -1 && column == -1)        row = rowCount(parent); // append    return view()->dropMimeData(item(parent), row, data, action);}Qt::DropActions QTreeModel::supportedDropActions() const{    return view()->supportedDropActions();}void QTreeModel::itemChanged(QTreeWidgetItem *item){    QModelIndex left = index(item, 0);    QModelIndex right = index(item, item->columnCount() - 1);    emit dataChanged(left, right);}bool QTreeModel::executePendingSort() const{    if (sortPending) {        sortPending = false;        int column = view()->header()->sortIndicatorSection();        Qt::SortOrder order = view()->header()->sortIndicatorOrder();        QTreeModel *that = const_cast<QTreeModel*>(this);        that->sort(column, order);        emit that->itemsSorted();        return true;    }    return false;}/*!  \internal    Emits the dataChanged() signal for the given \a item.    if column is -1 then all columns have changed*/void QTreeModel::emitDataChanged(QTreeWidgetItem *item, int column){    if (signalsBlocked())        return;    if (headerItem == item && column < item->columnCount()) {        if (column == -1)            emit headerDataChanged(Qt::Horizontal, 0, columnCount() - 1);        else            emit headerDataChanged(Qt::Horizontal, column, column);        return;    }    QModelIndex bottomRight, topLeft;    if (column == -1) {        topLeft = index(item, 0);        bottomRight = createIndex(topLeft.row(), columnCount() - 1, item);    } else {        topLeft = index(item, column);        bottomRight = topLeft;    }    emit dataChanged(topLeft, bottomRight);}void QTreeModel::beginInsertItems(QTreeWidgetItem *parent, int row, int count){    beginInsertRows(index(parent, 0), row, row + count - 1);}void QTreeModel::endInsertItems(){    endInsertRows();}void QTreeModel::beginRemoveItems(QTreeWidgetItem *parent, int row, int count){    Q_ASSERT(row >= 0);    Q_ASSERT(count > 0);    beginRemoveRows(index(parent, 0), row, row + count - 1);    if (!parent)        parent = rootItem;    // now update the iterators    for (int i = 0; i < iterators.count(); ++i) {        for (int j = 0; j < count; j++) {            QTreeWidgetItem *c = parent->child(row + j);            iterators[i]->d_func()->ensureValidIterator(c);        }    }}void QTreeModel::endRemoveItems(){    endRemoveRows();}void QTreeModel::sortItems(QList<QTreeWidgetItem*> *items, int column, Qt::SortOrder order){    Q_UNUSED(column);    // 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);    qStableSort(sorting.begin(), sorting.end(), compare);    int colCount = 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 brush which is set with  the setBackground() function. The current background brush can be  found with background().  The text label for each column can be rendered with its own font and brush.  These are specified with the setFont() and setForeground() functions,  and read with font() and foreground().  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 setCheckState() function. The corresponding checkState() function  indicates whether the item is currently checked.  \section1 Subclassing  When subclassing QTreeWidgetItem to provide custom items, it is possible to  define new types for them so that they can be distinguished from standard  items. The constructors for subclasses that require this feature need to  call the base class constructor with a new type value equal to or greater  than \l UserType.  \sa QTreeWidget, {Model/View Programming}, QListWidgetItem, QTableWidgetItem*//*!    \enum QTreeWidgetItem::ItemType    This enum describes the types that are used to describe tree widget items.    \value Type     The default type for tree widget items.    \value UserType The minimum value for custom types. Values below UserType are                    reserved by Qt.    You can define new user types in QTreeWidgetItem subclasses to ensure that    custom items are treated specially; for example, when items are sorted.    \sa type()*/

⌨️ 快捷键说明

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