如何 show/hide 在 QGraphicsScene 或 QGraphicsView 上绘制背景?
How can I show/hide background drawing on QGraphicsScene or QGraphicsView?
我想在 QGraphicsScene 上绘制某些东西,但不是 QGraphicsItem(它会干扰 QGraphicsItem
集合的处理)。
示例:场景边界矩形、网格
我为此目的覆盖了 drawBackground(QPainter *painter, const QRectF &rect)
。 (我应该将场景子类化...)
void MyView::showHideBounds()
{
m_showBackgroundBounds = !m_showBackgroundBounds;
// can't triger an update ???
update(); // neither does anything
viewport()->update();
}
void MyView::drawBackground(QPainter *painter, const QRectF &rect)
{
QPen pen;
if(m_showBackgroundBounds)
pen = QPen(QColor(0, 0, 0), 10, Qt::PenStyle(Qt::SolidLine));
else
pen = QPen(QColor(255, 255, 255), 10, Qt::PenStyle(Qt::SolidLine));
painter->setPen(pen);
painter->drawRect(QRect(QPoint(-scene()->sceneRect().size().toSize().width()/2,
-scene()->sceneRect().size().toSize().height()/2),
scene()->sceneRect().size().toSize()));
}
我想要 show/hide 边界矩形或网格的选项。
我唯一能想到的就是用背景画笔的颜色在它们上面画画?还有其他选择吗?
正如我在上面所写的那样,它可以工作——除非我需要用户对项目进行操作(或缩放或其他一些场景更改操作)以触发刷新,或调用更新...(函数 showHideBounds
没有 - 不确定如何让它强制刷新)
我会从 showHideBounds
函数调用 drawBackground
- 但我不知道如何获得 painter
[此外,drawBackground
似乎是自动绘制的...我怎样才能给它提供它需要的 rect
参数? (似乎如果我绘制 rect
它确实绘制了场景矩形,但我只看到右边缘和底部边缘)]
为了重绘场景的特定部分,您可以调用
QGraphicsScene->invalidate(rect_to_redraw, Backgroundlayer)
请注意,如果drawBackground(*painter, rect)
在矩形以外的区域绘制,它不会自动更新。在这种情况下,必须使用适当的 rect 参数调用 invalidate。
我想在 QGraphicsScene 上绘制某些东西,但不是 QGraphicsItem(它会干扰 QGraphicsItem
集合的处理)。
示例:场景边界矩形、网格
我为此目的覆盖了 drawBackground(QPainter *painter, const QRectF &rect)
。 (我应该将场景子类化...)
void MyView::showHideBounds()
{
m_showBackgroundBounds = !m_showBackgroundBounds;
// can't triger an update ???
update(); // neither does anything
viewport()->update();
}
void MyView::drawBackground(QPainter *painter, const QRectF &rect)
{
QPen pen;
if(m_showBackgroundBounds)
pen = QPen(QColor(0, 0, 0), 10, Qt::PenStyle(Qt::SolidLine));
else
pen = QPen(QColor(255, 255, 255), 10, Qt::PenStyle(Qt::SolidLine));
painter->setPen(pen);
painter->drawRect(QRect(QPoint(-scene()->sceneRect().size().toSize().width()/2,
-scene()->sceneRect().size().toSize().height()/2),
scene()->sceneRect().size().toSize()));
}
我想要 show/hide 边界矩形或网格的选项。
我唯一能想到的就是用背景画笔的颜色在它们上面画画?还有其他选择吗?
正如我在上面所写的那样,它可以工作——除非我需要用户对项目进行操作(或缩放或其他一些场景更改操作)以触发刷新,或调用更新...(函数 showHideBounds
没有 - 不确定如何让它强制刷新)
我会从 showHideBounds
函数调用 drawBackground
- 但我不知道如何获得 painter
[此外,drawBackground
似乎是自动绘制的...我怎样才能给它提供它需要的 rect
参数? (似乎如果我绘制 rect
它确实绘制了场景矩形,但我只看到右边缘和底部边缘)]
为了重绘场景的特定部分,您可以调用
QGraphicsScene->invalidate(rect_to_redraw, Backgroundlayer)
请注意,如果drawBackground(*painter, rect)
在矩形以外的区域绘制,它不会自动更新。在这种情况下,必须使用适当的 rect 参数调用 invalidate。