📄 qgraphicsscene.cpp
字号:
Returns the topmost visible item at the specified \a position, or 0 if there are no items at this position. \sa items(), collidingItems()*/QGraphicsItem *QGraphicsScene::itemAt(const QPointF &pos) const{ QList<QGraphicsItem *> itemsAtPoint = items(pos); return itemsAtPoint.isEmpty() ? 0 : itemsAtPoint.first();}/*! \fn QGraphicsScene::itemAt(qreal x, qreal y) const \overload Returns the topmost item at the position specified by (\a x, \a y), or 0 if there are no items at this position. This convenience function is equivalent to calling \c {itemAt(QPointF(x, y))}.*//*! Returns a list of all currently selected items. The items are returned in no particular order. \sa setSelectionArea()*/QList<QGraphicsItem *> QGraphicsScene::selectedItems() const{ Q_D(const QGraphicsScene); // Optimization: Lazily removes items that are not selected. QGraphicsScene *that = const_cast<QGraphicsScene *>(this); QSet<QGraphicsItem *> actuallySelectedSet; foreach (QGraphicsItem *item, that->d_func()->selectedItems) { if (item->isSelected()) actuallySelectedSet << item; } that->d_func()->selectedItems = actuallySelectedSet; return d->selectedItems.values();}/*! Returns the selection area that was previously set with setSelectionArea(), or an empty QPainterPath if no selection area has been set. \sa setSelectionArea()*/QPainterPath QGraphicsScene::selectionArea() const{ Q_D(const QGraphicsScene); return d->selectionArea;}/*! Sets the selection area to \a path. All items within this area are immediately selected, and all items outside are unselected. You can get the list of all selected items by calling selectedItems(). For an item to be selected, it must be marked as \e selectable (QGraphicsItem::ItemIsSelectable). \sa clearSelection(), selectionArea()*/void QGraphicsScene::setSelectionArea(const QPainterPath &path){ setSelectionArea(path, Qt::IntersectsItemShape);}/*! \overload \since 4.3 Sets the selection area to \a path using \a mode to determine if items are included in the selection area. \sa clearSelection(), selectionArea()*/void QGraphicsScene::setSelectionArea(const QPainterPath &path, Qt::ItemSelectionMode mode){ Q_D(QGraphicsScene); // Note: with boolean path operations, we can improve performance here // quite a lot by "growing" the old path instead of replacing it. That // allows us to only check the intersect area for changes, instead of // reevaluating the whole path over again. d->selectionArea = path; QSet<QGraphicsItem *> unselectItems = d->selectedItems; // Disable emitting selectionChanged() for individual items. ++d->selectionChanging; bool changed = false; // Set all items in path to selected. foreach (QGraphicsItem *item, items(path, mode)) { if (item->flags() & QGraphicsItem::ItemIsSelectable) { if (!item->isSelected()) changed = true; unselectItems.remove(item); item->setSelected(true); } } // Unselect all items outside path. foreach (QGraphicsItem *item, unselectItems) { item->setSelected(false); changed = true; } // Reenable emitting selectionChanged() for individual items. --d->selectionChanging; if (!d->selectionChanging && changed) emit selectionChanged();}/*! Clears the current selection. \sa setSelectionArea(), selectedItems()*/void QGraphicsScene::clearSelection(){ Q_D(QGraphicsScene); // Disable emitting selectionChanged ++d->selectionChanging; bool changed = !d->selectedItems.isEmpty(); foreach (QGraphicsItem *item, d->selectedItems) item->setSelected(false); d->selectedItems.clear(); // Reenable emitting selectionChanged() for individual items. --d->selectionChanging; if (!d->selectionChanging && changed) emit selectionChanged();}/*! Groups all items in \a items into a new QGraphicsItemGroup, and returns a pointer to the group. The group is created with the common ancestor of \a items as its parent, and with position (0, 0). The items are all reparented to the group, and their positions and transformations are mapped to the group. If \a items is empty, this function will return an empty top-level QGraphicsItemGroup. QGraphicsScene has ownership of the group item; you do not need to delete it. To dismantle (ungroup) a group, call destroyItemGroup(). \sa destroyItemGroup(), QGraphicsItemGroup::addToGroup()*/QGraphicsItemGroup *QGraphicsScene::createItemGroup(const QList<QGraphicsItem *> &items){ // Build a list of the first item's ancestors QList<QGraphicsItem *> ancestors; int n = 0; if (!items.isEmpty()) { QGraphicsItem *parent = items.at(n++); while ((parent = parent->parentItem())) ancestors.append(parent); } // Find the common ancestor for all items QGraphicsItem *commonAncestor = 0; if (!ancestors.isEmpty()) { while (n < items.size()) { int commonIndex = -1; QGraphicsItem *parent = items.at(n++); do { int index = ancestors.indexOf(parent, qMax(0, commonIndex)); if (index != -1) { commonIndex = index; break; } } while ((parent = parent->parentItem())); if (commonIndex == -1) { commonAncestor = 0; break; } commonAncestor = ancestors.at(commonIndex); } } // Create a new group at that level QGraphicsItemGroup *group = new QGraphicsItemGroup(commonAncestor); if (!commonAncestor) addItem(group); foreach (QGraphicsItem *item, items) group->addToGroup(item); return group;}/*! Reparents all items in \a group to \a group's parent item, then removes \a group from the scene, and finally deletes it. The items' positions and transformations are mapped from the group to the group's parent. \sa createItemGroup(), QGraphicsItemGroup::removeFromGroup()*/void QGraphicsScene::destroyItemGroup(QGraphicsItemGroup *group){ foreach (QGraphicsItem *item, group->children()) group->removeFromGroup(item); removeItem(group); delete group;}/*! Adds or moves the item \a item and all its childen to the scene. If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop. If the item is already in a different scene, it will first be removed from its old scene, and then added to this scene as a top-level. \sa removeItem(), addEllipse(), addLine(), addPath(), addPixmap(), addRect(), addText()*/void QGraphicsScene::addItem(QGraphicsItem *item){ Q_D(QGraphicsScene); if (!item) { qWarning("QGraphicsScene::addItem: cannot add null item"); return; } if (item->scene() == this) { qWarning("QGraphicsScene::addItem: item has already been added to this scene"); return; } // Prevent reusing a recently deleted pointer: purge all removed items // from our lists. d->purgeRemovedItems(); // Remove this item from its existing scene if (item->d_func()->scene) item->d_func()->scene->removeItem(item); // Detach this item from its parent if the parent's scene is different // from this scene. if (QGraphicsItem *itemParent = item->parentItem()) { if (itemParent->scene() != this) { QVariant variant; qVariantSetValue<QGraphicsItem *>(variant, item); itemParent->itemChange(QGraphicsItem::ItemChildRemovedChange, variant); itemParent->d_func()->children.removeAll(item); item->d_func()->parent = 0; } } // Add the item to this scene item->d_func()->scene = qVariantValue<QGraphicsScene *>(item->itemChange(QGraphicsItem::ItemSceneChange, qVariantFromValue<QGraphicsScene *>(this))); if (d->indexMethod != QGraphicsScene::NoIndex) { // Indexing requires sceneBoundingRect(), but because \a item might // not be completely constructed at this point, we need to store it in // a temporary list and schedule an indexing for later. d->unindexedItems << item; item->d_func()->index = -1; d->startIndexTimer(); } else { // No index: We can insert the item directly. if (!d->freeItemIndexes.isEmpty()) { int newIndex = d->freeItemIndexes.takeLast(); Q_ASSERT_X(d->indexedItems[newIndex] == 0, "QGraphicsItem::addItem", "An index marked as free was still occupied"); d->indexedItems[newIndex] = item; item->d_func()->index = newIndex; } else { item->d_func()->index = d->indexedItems.size(); d->indexedItems << item; } } // Add to list of items that require an update. We cannot assume that the // item is fully constructed, so calling item->update() can lead to a pure // virtual function call to boundingRect(). if (!d->updateAll) { if (d->pendingUpdateItems.isEmpty()) QTimer::singleShot(0, this, SLOT(_q_updateLater())); d->pendingUpdateItems << item; } // Disable selectionChanged() for individual items ++d->selectionChanging; int oldSelectedItemSize = d->selectedItems.size(); // Update selection lists if (item->isSelected()) d->selectedItems << item; // Add all children recursively foreach (QGraphicsItem *child, item->children()) addItem(child); // Reenable selectionChanged() for individual items --d->selectionChanging; if (!d->selectionChanging && d->selectedItems.size() != oldSelectedItemSize) emit selectionChanged();}/*! Creates and adds an ellipse item to the scene, and returns the item pointer. The geometry of the ellipse is defined by \a rect, and it's pen and brush are initialized to \a pen and \a brush. Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0). If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop. \sa addLine(), addPath(), addPixmap(), addRect(), addText(), addItem()*/QGraphicsEllipseItem *QGraphicsScene::addEllipse(const QRectF &rect, const QPen &pen, const QBrush &brush){ QGraphicsEllipseItem *item = new QGraphicsEllipseItem(rect); item->setPen(pen); item->setBrush(brush); addItem(item); return item;}/*! \fn QGraphicsEllipseItem *QGraphicsScene::addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen, const QBrush &brush) \since 4.3 This convenience function is equivalent to calling addEllipse(QRectF(\a x, \a y, \a w, \a h), \a pen, \a brush).*//*! Creates and adds a line item to the scene, and returns the item pointer. The geometry of the line is defined by \a line, and it's pen is initialized to \a pen. Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0). If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop. \sa addEllipse(), addPath(), addPixmap(), addRect(), addText(), addItem()*/QGraphicsLineItem *QGraphicsScene::addLine(const QLineF &line, const QPen &pen){ QGraphicsLineItem *item = new QGraphicsLineItem(line); item->setPen(pen); addItem(item); return item;}/*! \fn QGraphicsLineItem *QGraphicsScene::addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen) \since 4.3 This convenience function is equivalent to calling addLine(QLineF(\a x1, \a y1, \a x2, \a y2), \a pen).*//*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -