Unsolved Erase QGraphicsRectItem
-
I ve got custom GraphicsRectItem which inherits QGraphicsRectItem. I paint on GraphicsRectItem using paint() - function, but sometimes i need erase it. How can i do it not using paint() - function?
-
Hi
What do yo mean by erase it ? Not delete the item , but remove any "paint"?
If you mean whatever you draw with QPainter, then you need to alter
the paint function to draw empty rect to clear it. -
Hello. I had to create bool-value and i called it "IN". I override hoverLeaveEvent and hoverEnterEvent like this
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
in = false;
}void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
in = true;
}void paint(QPainter *painter, QStyleoOptionGraphicsItem option, QWidget widget = nullptr)
{
if (in)
{
// draw something
}
}it works but , as for me, it is bad way. Or not? Sometimes i ve got trouble. I ve got QGraphicsScene and custom GraphicsRectItem and when my button pressed hoverLeaveEvent and hoverEnterEvent doesnt work because of scene that grabbed mouse.
-
Hi
Well its not a bad way as such but if enter/leave is the only way its controlled
then yes, it has issues with buttons as they grab the mouse.Im not sure what you use it for but if it should only be shown when you enter and
"removed" when you leave, then i think you need to grab the mouse to be sure it happens. -
I m not sure that can you understand you. Can you describe your idea by code ?
-
Hi
I mean try to call
https://doc.qt.io/qt-5/qwidget.html#grabMouse in hoverEnterEvent
and https://doc.qt.io/qt-5/qwidget.html#releaseMouse in hoverLeaveEvent
to try to make sure you get the leave event to have it not draw anything. -
I ve got custom GraphicsItem. Well
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
ungrabMouse();
Q_UNUSED(event);
in = false;
for(int i = 0; i < 4; ++i)
update(targets[i]);
}virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) { grabMouse(); Q_UNUSED(event); in = true; for(int i = 0; i < 4; ++i) update(targets[i]); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) { if (in) { if (Action::activeAction == Action::Text) { painter->setRenderHint(QPainter::SmoothPixmapTransform); for(int i = 0; i < 4; ++i) painter->drawPixmap(targets[i],arrows.at(i),QRect(0,0,targets[i].width(),targets[i].height())); QRectF rectText(indent,indent, proxy->rect().width()*proxy->scale(), proxy->rect().height()*proxy->scale()); painter->drawRoundRect(rectText); } else if (Action::activeAction == Action::Line) { painter->setPen(Qt::red); painter->drawRect(rect()); } } }
when i my mouse inside of GraphicsItem then it grabs mouse but when i leave GraphicsItem it doesnt ungrabMouse. But if mouse leave window of my application then it ungrabs mouse.
-
Hi
So hoverLeaveEvent do not fire for your GraphicsItem, even you grab the mouse? -
When i clicked not on GraphicsItem and moved into GraphicsItem. hoverLeaveEvent and hoverEnterEvent doesnt work, but if i moving into GraphicsItem without pressing hoverLeaveEvent and hoverEnterEvent are working. This my code
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event); in = false; for(int i = 0; i < 4; ++i) update(targets[i]); } virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event); in = true; for(int i = 0; i < 4; ++i) update(targets[i]); }
-
Hi
What do you click on first when it doesn't work ? -
I clicked on where the point( for example ) and move into
Text
( It is GraphicsItem with QTextEdit ) then hoverEnterEvent doesnt work -
@Kaznachei
Hmm, i dont know why.
The TextEdit has the focus but that would not normally block enterEvent for other
Widgets but maybe its a side effect of mixing it with GraphicsItems.