📄 qgraphicsitem.cpp
字号:
Returns a pointer to this item's parent item. If this item does not have a parent, 0 is returned. \sa setParentItem(), children()*/QGraphicsItem *QGraphicsItem::parentItem() const{ return d_ptr->parent;}/*! Returns this item's top-level item. The top-level item is the item's topmost ancestor item whose parent is 0. If an item has no parent, its own pointer is returned (i.e., a top-level item is its own top-level item). \sa parentItem()*/QGraphicsItem *QGraphicsItem::topLevelItem() const{ QGraphicsItem *parent = const_cast<QGraphicsItem *>(this); while (QGraphicsItem *grandPa = parent->parentItem()) parent = grandPa; return parent;}/*! Sets this item's parent item to \a parent. If this item already has a parent, it is first removed from the previous parent. If \a parent is 0, this item will become a top-level item. \sa parentItem(), children()*/void QGraphicsItem::setParentItem(QGraphicsItem *parent){ if (parent == this) { qWarning("QGraphicsItem::setParentItem: cannot assign %p as a parent of itself", this); return; } if (parent == d_ptr->parent) return; QVariant variant; qVariantSetValue<QGraphicsItem *>(variant, parent); parent = qVariantValue<QGraphicsItem *>(itemChange(ItemParentChange, variant)); if (parent == d_ptr->parent) return; // We anticipate geometry changes prepareGeometryChange(); if (d_ptr->parent) { // Remove from current parent d_ptr->parent->d_func()->children.removeAll(this); qVariantSetValue<QGraphicsItem *>(variant, this); d_ptr->parent->itemChange(ItemChildRemovedChange, variant); } if ((d_ptr->parent = parent)) { bool implicitUpdate = false; if (parent->d_func()->scene && parent->d_func()->scene != d_ptr->scene) { // Move this item to its new parent's scene parent->d_func()->scene->addItem(this); implicitUpdate = true; } else if (!parent->d_func()->scene && d_ptr->scene) { // Remove this item from its former scene d_ptr->scene->removeItem(this); } d_ptr->parent->d_func()->children << this; qVariantSetValue<QGraphicsItem *>(variant, this); d_ptr->parent->itemChange(ItemChildAddedChange, variant); if (!implicitUpdate) update(); // Inherit ancestor flags from the new parent. d_ptr->updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-1)); d_ptr->updateAncestorFlag(ItemClipsChildrenToShape); d_ptr->updateAncestorFlag(ItemIgnoresTransformations); // Update item visible / enabled. if (d_ptr->parent->isVisible() != d_ptr->visible) { if (!d_ptr->parent->isVisible() || !d_ptr->explicitlyHidden) d_ptr->setVisibleHelper(d_ptr->parent->isVisible(), /* explicit = */ false, /* update = */ !implicitUpdate); } if (d_ptr->parent->isEnabled() != d_ptr->enabled) { if (!d_ptr->parent->isEnabled() || !d_ptr->explicitlyDisabled) d_ptr->setEnabledHelper(d_ptr->parent->isEnabled(), /* explicit = */ false, /* update = */ !implicitUpdate); } } else { // Inherit ancestor flags from the new parent. d_ptr->updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-1)); d_ptr->updateAncestorFlag(ItemClipsChildrenToShape); d_ptr->updateAncestorFlag(ItemIgnoresTransformations); // Update item visible / enabled. if (!d_ptr->visible && !d_ptr->explicitlyHidden) d_ptr->setVisibleHelper(true, /* explicit = */ false); if (!d_ptr->enabled && !d_ptr->explicitlyDisabled) d_ptr->setEnabledHelper(true, /* explicit = */ false); update(); }}/*! Returns a list of this item's children. The items are returned in no particular order. \sa setParentItem()*/QList<QGraphicsItem *> QGraphicsItem::children() const{ return d_ptr->children;}/*! Returns this item's flags. The flags describe what configurable features of the item are enabled and not. For example, if the flags include ItemIsFocusable, the item can accept input focus. By default, no flags are enabled. \sa setFlags(), setFlag()*/QGraphicsItem::GraphicsItemFlags QGraphicsItem::flags() const{ return GraphicsItemFlags(d_ptr->flags);}/*! If \a enabled is true, the item flag \a flag is enabled; otherwise, it is disabled. \sa flags(), setFlags()*/void QGraphicsItem::setFlag(GraphicsItemFlag flag, bool enabled){ if (enabled) setFlags(flags() | flag); else setFlags(flags() & ~flag);}/*! \internal Sets the flag \a flag on \a item and all its children, to \a enabled.*/static void _q_qgraphicsItemSetFlag(QGraphicsItem *item, QGraphicsItem::GraphicsItemFlag flag, bool enabled){ if (item->flags() & flag) { // If this item already has the correct flag set, we don't have to // propagate it. return; } item->setFlag(flag, enabled); foreach (QGraphicsItem *child, item->children()) _q_qgraphicsItemSetFlag(child, flag, enabled);}/*! Sets the item flags to \a flags. All flags in \a flags are enabled; all flags not in \a flags are disabled. If the item had focus and \a flags does not enable ItemIsFocusable, the item loses focus as a result of calling this function. Similarly, if the item was selected, and \a flags does not enabled ItemIsSelectable, the item is automatically unselected. By default, no flags are enabled. \sa flags(), setFlag()*/void QGraphicsItem::setFlags(GraphicsItemFlags flags){ if (GraphicsItemFlags(d_ptr->flags) != flags) { GraphicsItemFlags oldFlags = GraphicsItemFlags(d_ptr->flags); d_ptr->flags = flags; if (!(d_ptr->flags & ItemIsFocusable) && hasFocus()) { // Clear focus on the item if it has focus when the focusable flag // is unset. clearFocus(); } if (!(d_ptr->flags & ItemIsSelectable) && isSelected()) { // Unselect the item if it is selected when the selectable flag is // unset. setSelected(false); } if ((flags & ItemClipsChildrenToShape) != (oldFlags & ItemClipsChildrenToShape)) { // Item children clipping changes. Propagate the ancestor flag to // all children. d_ptr->updateAncestorFlag(ItemClipsChildrenToShape); } if ((flags & ItemIgnoresTransformations) != (oldFlags & ItemIgnoresTransformations)) { // Item children clipping changes. Propagate the ancestor flag to // all children. d_ptr->updateAncestorFlag(ItemIgnoresTransformations); } update(); }}#ifndef QT_NO_TOOLTIP/*! Returns the item's tool tip, or an empty QString if no tool tip has been set. \sa setToolTip(), QToolTip*/QString QGraphicsItem::toolTip() const{ return d_ptr->extra(QGraphicsItemPrivate::ExtraToolTip).toString();}/*! Sets the item's tool tip to \a toolTip. If \a toolTip is empty, the item's tool tip is cleared. \sa toolTip(), QToolTip*/void QGraphicsItem::setToolTip(const QString &toolTip){ d_ptr->setExtra(QGraphicsItemPrivate::ExtraToolTip, toolTip);}#endif // QT_NO_TOOLTIP#ifndef QT_NO_CURSOR/*! Returns the current cursor shape for the item. The mouse cursor will assume this shape when it's over this item. See the \link Qt::CursorShape list of predefined cursor objects\endlink for a range of useful shapes. An editor item might want to use an I-beam cursor: \code item->setCursor(Qt::IBeamCursor); \endcode If no cursor has been set, the parent's cursor is used. \sa setCursor(), hasCursor(), unsetCursor(), QWidget::cursor, QApplication::overrideCursor()*/QCursor QGraphicsItem::cursor() const{ return qVariantValue<QCursor>(d_ptr->extra(QGraphicsItemPrivate::ExtraCursor));}/*! Sets the current cursor shape for the item to \a cursor. The mouse cursor will assume this shape when it's over this item. See the \link Qt::CursorShape list of predefined cursor objects\endlink for a range of useful shapes. An editor item might want to use an I-beam cursor: \code item->setCursor(Qt::IBeamCursor); \endcode If no cursor has been set, the cursor of the item beneath is used. \sa cursor(), hasCursor(), unsetCursor(), QWidget::cursor, QApplication::overrideCursor()*/void QGraphicsItem::setCursor(const QCursor &cursor){ d_ptr->setExtra(QGraphicsItemPrivate::ExtraCursor, cursor); d_ptr->hasCursor = 1; if (d_ptr->scene) { foreach (QGraphicsView *view, d_ptr->scene->views()) { // Note: Some of this logic is duplicated in QGraphicsView's mouse events. if (view->underMouse()) { foreach (QGraphicsItem *itemUnderCursor, view->items(view->mapFromGlobal(QCursor::pos()))) { if (itemUnderCursor->hasCursor()) { QMetaObject::invokeMethod(view, "_q_setViewportCursor", Q_ARG(QCursor, itemUnderCursor->cursor())); break; } } break; } } }}/*! Returns true if this item has a cursor set; otherwise, false is returned. By default, items don't have any cursor set. cursor() will return a standard pointing arrow cursor. \sa unsetCursor()*/bool QGraphicsItem::hasCursor() const{ return d_ptr->hasCursor;}/*! Clears the cursor from this item. \sa hasCursor(), setCursor()*/void QGraphicsItem::unsetCursor(){ d_ptr->unsetExtra(QGraphicsItemPrivate::ExtraCursor); d_ptr->hasCursor = 0; if (d_ptr->scene) { foreach (QGraphicsView *view, d_ptr->scene->views()) { if (view->underMouse() && view->itemAt(view->mapFromGlobal(QCursor::pos())) == this) { QMetaObject::invokeMethod(view, "_q_unsetViewportCursor"); break; } } }}#endif // QT_NO_CURSOR/*! Returns true if the item is visible; otherwise, false is returned. Note that the item's general visibility is unrelated to whether or not it is actually being visualized by a QGraphicsView. \sa setVisible()*/bool QGraphicsItem::isVisible() const{ return d_ptr->visible;}/*! \internal Sets this item's visibility to \a newVisible. If \a explicitly is true, this item will be "explicitly" \a newVisible; otherwise, it.. will not be.*/void QGraphicsItemPrivate::setVisibleHelper(bool newVisible, bool explicitly, bool update){ // Update explicit bit. if (explicitly) explicitlyHidden = newVisible ? 0 : 1; // Check if there's nothing to do. if (visible == quint32(newVisible)) return; // Certain properties are dropped as an item becomes invisible. if (!newVisible) { if (scene && scene->mouseGrabberItem() == q_ptr) scene->d_func()->mouseGrabberItem = 0; if (q_ptr->hasFocus()) q_ptr->clearFocus(); if (q_ptr->isSelected()) q_ptr->setSelected(false); } // Schedule redrawing, and modify the property. if (update && !newVisible) q_ptr->update(); visible = q_ptr->itemChange(QGraphicsItem::ItemVisibleChange, quint32(newVisible)).toBool(); if (update && newVisible)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -