Qt update()不工作 [英] Qt update() doesn't work
问题描述
我有一个问题,在 QGraphicsItem
中的update()函数不工作。我想做的是,当我移动圆,其他 QGraphicsItem
(在平均时间roundrect)改变颜色。
这是一个例子,我想做什么:
I have a problem , that update() function in QGraphicsItem
doesn't work. What I want to do is , when I move circle , other QGraphicsItem
( in the mean time roundrect ) changes color.
This is a example, what I want to do:
circle.cpp:
circle.cpp:
void CircleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// RoundRect Object
RectItem->SetBackGround();
QGraphicsItem::mouseMoveEvent( event );
}
RoundRect.cpp:
RoundRect.cpp:
void RoundRectItem::SetBackGround()
{
ChangeBackground = true;
update();
}
void RoundRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec = QRectF( QPoint( 0,0 ), boundingRect().size() / 2 );
roundRect = QRectF(rec.adjusted(-rec.height() / 2, 0, rec.height()/2, 0));
roundRect.moveTo( boundingRect().center().x() - roundRect.width() / 2,
boundingRect().center().y() - roundRect.height() / 2 );
if( !ChangeBackground )
painter->setBrush( backBrush );
else
painter->setBrush( QBrush( Qt::blue ) );
painter->setPen( QColor( 255,255,255 ) );
painter->drawRoundedRect(roundRect, roundRect.height() / 2, roundRect.height() / 2 );
}
$ b <
So the question is, how can I make this update()
work right.
推荐答案
正在调用QGraphicsItem的update()方法。你应该调用你正在使用的QGraphicsView的update()。例如,你可以保持你的QGraphicsView作为你的项目的成员类,如:
You are invoking the update() method of QGraphicsItem. You should call the update() of the QGraphicsView you are working with. For example you can keep your QGraphicsView as a member class of your item like:
QGraphicsView * parent;
当您希望更改发生时调用它的更新方法:
And call it's update method when you want the changes take place like:
void RoundRectItem::SetBackGround()
{
ChangeBackground = true;
parent->update();
}
这篇关于Qt update()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!