我正在SizeGripItem
的子类上使用here中的QGraphicsRectItem
(经过最小的修改以添加信号),
//! RectItem that sends a boxChanged signal whenever it is moved or resized via the handles.
class QSignalingBoxItem : public QObject, public QGraphicsRectItem
{
Q_OBJECT
private:
void setSelected_(bool selected);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant & value);
public:
QSignalingBoxItem(QRectF rect, QGraphicsItem * parent = nullptr);
~QSignalingBoxItem();
signals:
void boxChanged(QRectF newBox);
};
namespace
{
class BoxResizer : public SizeGripItem::Resizer
{
public:
virtual void operator()(QGraphicsItem* item, const QRectF& rect)
{
QSignalingBoxItem* rectItem =
dynamic_cast<QSignalingBoxItem*>(item);
if (rectItem)
{
rectItem->setRect(rect);
}
}
};
}
QSignalingBoxItem::QSignalingBoxItem(QRectF rect, QGraphicsItem * parent) : QGraphicsRectItem(parent)
{
setFlags(QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemSendsScenePositionChanges);
}
QVariant QSignalingBoxItem::itemChange(GraphicsItemChange change, const QVariant & value)
{
switch (change)
{
case QGraphicsItem::ItemSelectedHasChanged:
setSelected_(value.toBool());
break;
case QGraphicsItem::ItemScenePositionHasChanged:
emit boxChanged(rect());
break;
default:
break;
}
return value;
}
void QSignalingBoxItem::setSelected_(bool selected)
{
//TODO: Test that it works as expected
if (selected)
{
auto sz = new SizeGripItem(new BoxResizer, this);
connect(sz, &SizeGripItem::resized, [&]() {emit boxChanged(rect()); });
}
else
{
// Get the child
// If it's a SizeGripItem, delete it.
if (childItems().length() > 0)
{
foreach(QGraphicsItem* item, childItems())
{
auto sz = dynamic_cast<SizeGripItem*>(item);
if (sz)
{
delete sz;
break;
}
}
}
}
}
如果我在构造函数中应用SizeGripItem
,则该类将按预期工作。但是,我需要使句柄可见并且仅在选中该项目时才起作用。但是,当我运行上面的代码时,会显示手柄,但单击手柄会移动整个框(就像我单击框中间一样),而不是调整其大小。
在调试器上运行,我发现itemChange
甚至都没有被调用。
为什么会这样呢?选中该项目后,如何修改类以使其正常工作?
这是使用该类的方式。我有一个QGraphicsView
的扩展名及其专用的m_scene
,在其中我覆盖了mouse{Press,Move,Release}Event
,以通过“单击并拖动”生成一个框。
撇开该类的其余部分,因为它涉及到QSignalingBoxItem
的唯一部分是这三个函数,所以我添加了它们。如果需要更多,我将根据需要添加。
void QSampleEditor::mousePressEvent(QMouseEvent * mouseEvent)
{
QImageView::mousePressEvent(mouseEvent);
if (mouseEvent->buttons() == Qt::RightButton && m_currentSample && m_activePixmap && m_activePixmap->isUnderMouse())
{
m_dragStart = mapToScene(mouseEvent->pos());
m_currentBox = new QSignalingBoxItem({ m_dragStart, QSize(0,0) });
m_scene.addItem(m_currentBox);
mouseEvent->accept();
}
}
void QSampleEditor::mouseMoveEvent(QMouseEvent * mouseEvent)
{
QImageView::mouseMoveEvent(mouseEvent);
if (m_currentBox)
{
m_currentBox->setRect(rectFromTwoPoints(m_dragStart, mapToScene(mouseEvent->pos())));
mouseEvent->accept();
}
}
void QSampleEditor::mouseReleaseEvent(QMouseEvent * mouseEvent)
{
//TODO: Add checks that the sample is still there.
QImageView::mouseReleaseEvent(mouseEvent);
if (m_currentBox)
{
// Add to the StringSample
auto new_char = new CharacterSample;
connect(m_currentBox, &QSignalingBoxItem::boxChanged, new_char, &CharacterSample::boxChanged);
new_char->boxChanged(m_currentBox->rect());
m_currentSample->addCharacter(new_char);
m_currentBox = nullptr;
mouseEvent->accept();
}
}
答案 0 :(得分:1)
当给定项目的任何可移动祖先项目被选中时,最老的被选择祖先将接收事件-即使该项目的位置与子项目重叠。这在QGraphicsItem::mouseMoveEvent
中进行了处理-如果存在可移动的祖先,则不会处理move事件。最老的可移动选定祖先接收到该事件并将其用于移动自身,但后代项将忽略该事件(这里是句柄!)。
有关代码(您和您正在重用的代码)的一般说明:
以Q
开头的类型名称保留用于Qt。除非您已将Qt放在命名空间中,否则不应使用此类名称。
应该将SizeGripItem
标记为不包含任何内容,因为其paint
方法是无操作的。
通过const引用传递非数字,非指针方法参数除非,该方法需要一个副本才能在内部进行修改。
SizeGripItem
必须具有Resizer
或发出信号,而不是两者都要-这两个选项是互斥的。
实际上,Resizer
是Qt 4的痕迹,其中的槽位很冗长,无法连接到lambda。在Qt 5中完全没有必要将信号连接到任何函子,包括Resizer
类型的函子,从而使QObject::connect
与显式使用{{1}隐式地向后兼容。 }(!)。
可以提出以下解决方案:
Resizer
移动本身。 SizeGripItem
无法移动。SignalingBoxItem
的{{1}},以接受相关事件并对其做出反应。 SizeGripItem::HandleItem
仍可移动。mouseMoveEvent
用作其SignalingBoxItem
的事件过滤器,并像解决方案2中一样处理相关事件。 SizeGripItem
仍可移动。HandleItem
是SignalingBoxItem
的兄弟姐妹。这样SizeGripItem
就可以移动了,而不会影响SignalingBoxItem
的句柄的使用。当然,SignalingBoxItem
的祖先就不能动了。以下是约240行的完整示例,实现了解决方案1-3。每个解决方案都在条件块中进行了描述,并且示例在启用它们的任何子集的情况下进行编译。没有选择解决方案,原来的问题仍然存在。可以在运行时选择启用的解决方案。
首先,让我们从SizeGripItem
开始:
SizeGripItem
然后,SignalingBoxItem
:
// https://github.com/KubaO/stackoverflown/tree/master/questions/graphicsscene-children-51596611
#include <QtWidgets>
#include <array>
#define SOLUTION(s) ((!!(s)) << (s))
#define HAS_SOLUTION(s) (!!(SOLUTIONS & SOLUTION(s)))
#define SOLUTIONS (SOLUTION(1) | SOLUTION(2) | SOLUTION(3))
class SizeGripItem : public QGraphicsObject {
Q_OBJECT
enum { kMoveInHandle, kInitialPos, kPressPos };
struct HandleItem : QGraphicsRectItem {
HandleItem() : QGraphicsRectItem(-4, -4, 8, 8) {
setBrush(Qt::lightGray);
setFlags(ItemIsMovable | ItemSendsGeometryChanges);
}
SizeGripItem *parent() const { return static_cast<SizeGripItem *>(parentItem()); }
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override {
if (change == ItemPositionHasChanged) parent()->handleMoved(this);
return value;
}
#if HAS_SOLUTION(2)
bool sceneEvent(QEvent *event) override {
return (data(kMoveInHandle).toBool() && hasSelectedMovableAncestor(this) &&
processMove(this, event)) ||
QGraphicsRectItem::sceneEvent(event);
}
#endif
};
#if HAS_SOLUTION(2) || HAS_SOLUTION(3)
static bool processMove(QGraphicsItem *item, QEvent *ev) {
auto mev = static_cast<QGraphicsSceneMouseEvent *>(ev);
if (ev->type() == QEvent::GraphicsSceneMousePress &&
mev->button() == Qt::LeftButton) {
item->setData(kInitialPos, item->pos());
item->setData(kPressPos, item->mapToParent(mev->pos()));
return true;
} else if (ev->type() == QEvent::GraphicsSceneMouseMove &&
mev->buttons() == Qt::LeftButton) {
auto delta = item->mapToParent(mev->pos()) - item->data(kPressPos).toPointF();
item->setPos(item->data(kInitialPos).toPointF() + delta);
return true;
}
return false;
}
static bool hasSelectedMovableAncestor(const QGraphicsItem *item) {
auto *p = item->parentItem();
return p && ((p->isSelected() && (p->flags() & QGraphicsItem::ItemIsMovable)) ||
hasSelectedMovableAncestor(p));
}
#endif
std::array<HandleItem, 4> handles_;
QRectF rect_;
void updateHandleItemPositions() {
static auto get = {&QRectF::topLeft, &QRectF::topRight, &QRectF::bottomLeft,
&QRectF::bottomRight};
for (auto &h : handles_) h.setPos((rect_.*get.begin()[index(&h)])());
}
int index(HandleItem *handle) const { return handle - &handles_[0]; }
void handleMoved(HandleItem *handle) {
static auto set = {&QRectF::setTopLeft, &QRectF::setTopRight,
&QRectF::setBottomLeft, &QRectF::setBottomRight};
auto rect = rect_;
(rect.*set.begin()[index(handle)])(handle->pos());
setRect(mapRectToParent(rect.normalized()));
}
public:
SizeGripItem(QGraphicsItem *parent = {}) : QGraphicsObject(parent) {
for (auto &h : handles_) h.setParentItem(this);
setFlags(ItemHasNoContents);
}
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override {
if (change == QGraphicsItem::ItemPositionHasChanged) resize();
return value;
}
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override {}
QRectF boundingRect() const override { return rect_; }
void setRect(const QRectF &rect) {
rect_ = mapRectFromParent(rect);
resize();
updateHandleItemPositions();
}
void resize() { emit rectChanged(mapRectToParent(rect_), parentItem()); }
Q_SIGNAL void rectChanged(const QRectF &, QGraphicsItem *);
#if SOLUTIONS
void selectSolution(int i) {
#if HAS_SOLUTION(1)
setFlag(ItemIsMovable, i == 1);
setFlag(ItemSendsGeometryChanges, i == 1);
if (i != 1) {
auto rect = mapRectToParent(rect_);
setPos({}); // reset position if we're leaving the movable mode
setRect(rect);
}
i--;
#endif
for (auto &h : handles_) {
int ii = i;
#if HAS_SOLUTION(2)
h.setData(kMoveInHandle, ii-- == 1);
#endif
#if HAS_SOLUTION(3)
if (ii == 1)
h.installSceneEventFilter(this);
else
h.removeSceneEventFilter(this);
#endif
}
}
#endif
#if HAS_SOLUTION(3)
bool sceneEventFilter(QGraphicsItem *item, QEvent *ev) override {
if (hasSelectedMovableAncestor(item)) return processMove(item, ev);
return false;
}
#endif
};
SignalingBoxItem
:
class SignalingBoxItem : public QObject, public QGraphicsRectItem {
Q_OBJECT
SizeGripItem m_sizeGrip{this};
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override {
if (change == QGraphicsItem::ItemSelectedHasChanged)
m_sizeGrip.setVisible(value.toBool());
else if (change == QGraphicsItem::ItemScenePositionHasChanged)
emitRectChanged();
return value;
}
void emitRectChanged() { emit rectChanged(mapRectToScene(rect())); }
void setRectImpl(const QRectF &rect) {
QGraphicsRectItem::setRect(rect);
emitRectChanged();
}
public:
SignalingBoxItem(const QRectF &rect = {}, QGraphicsItem *parent = {})
: QGraphicsRectItem(rect, parent) {
setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsScenePositionChanges);
m_sizeGrip.hide();
connect(&m_sizeGrip, &SizeGripItem::rectChanged, this,
&SignalingBoxItem::setRectImpl);
}
void setRect(const QRectF &rect) {
setSelected(false);
m_sizeGrip.setRect(rect);
setRectImpl(rect);
}
Q_SIGNAL void rectChanged(const QRectF &); // Rectangle in scene coordinates
#if SOLUTIONS
void selectSolution(int index) {
setFlag(ItemIsMovable, !HAS_SOLUTION(1) || index != 1);
m_sizeGrip.selectSolution(index);
}
#endif
};
最后是演示代码:
SampleEditor