急求怎么解决ItemPositonHasChanged无响应的有关问题
急求如何解决ItemPositonHasChanged无响应的问题?
小弟最近在学习Qt编程,学习到第八章QGraphicsItem时碰到了一个棘手的问题:在Item拖动的时候GraphicsItemChange中的ItemPositonHasChanged没有响应,导致了拖动节点时,连线无法更新.我是按照书上给的例子写的.随书源码也是同样的问题.注:编译环境Qt 5.4.0
其中node.cpp如下:在itemChange函数中
------解决思路----------------------
多看看Manual
The item's position has changed. This notification is sent if the ItemSendsGeometryChanges flag is enabled, and after the item's local position, relative to its parent, has changed. The value argument is the new position (the same as pos()), and QGraphicsItem ignores the return value for this notification (i.e., a read-only notification).
小弟最近在学习Qt编程,学习到第八章QGraphicsItem时碰到了一个棘手的问题:在Item拖动的时候GraphicsItemChange中的ItemPositonHasChanged没有响应,导致了拖动节点时,连线无法更新.我是按照书上给的例子写的.随书源码也是同样的问题.注:编译环境Qt 5.4.0
其中node.cpp如下:在itemChange函数中
#include <node.h>
Node::Node()
{
myTextColor=Qt::darkGreen;
myOutlineColor=Qt::darkBlue;
myBackgroundColor=Qt::white;
setFlags(ItemIsMovable|ItemIsSelectable);
}
Node::~Node()
{
foreach (Link *link, myLinks) {
delete link;
}
}
void Node::setText(const QString &text)
{
prepareGeometryChange();
myText=text;
update();//查看修改结果
}
QString Node::text()const
{
return myText;
}
void Node::setTextColor(const QColor &color)
{
myTextColor=color;
update();
}
QColor Node::textColor()const
{
return myTextColor;
}
void Node::setOutlineColor(const QColor &color)
{
myOutlineColor=color;
update();
}
QColor Node::outlineColor()const
{
return myOutlineColor;
}
void Node::setBackgroundColor(const QColor &color)
{
myBackgroundColor=color;
update();
}
QColor Node::backgroundColor()const
{
return myBackgroundColor;
}
void Node::addLink(Link *link)
{
myLinks.insert(link);
}
void Node::removeLink(Link *link)
{
myLinks.remove(link);
}
//boundingRect()由QGraphicsView调用,以决定是否需要绘制项
//什么意思?
QRectF Node::boundingRect()const
{
const int Margin=1;
return (outlineRect().adjusted(-Margin,-Margin,+Margin,+Margin));
}
//用来做精确的碰撞检测,当鼠标在边框外时,不选中边框.
QPainterPath Node::shape()const
{
QRectF rect=outlineRect();
QPainterPath path;
path.addRoundedRect(rect,roundness(rect.width()),roundness(rect.height()),Qt::RelativeSize);
return path;
}
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/)
{
QPen pen(myOutlineColor);
if(option->state & QStyle::State_Selected){
pen.setStyle(Qt::DotLine);
pen.setWidth(2);
}
painter->setPen(pen);
painter->setBrush(myBackgroundColor);
QRectF rect=outlineRect();
painter->drawRoundedRect(rect,roundness(rect.width()),
roundness((rect.height())),Qt::RelativeSize);
painter->setPen(myTextColor);
painter->drawText(rect,Qt::AlignCenter,myText);
}
void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
QString text=QInputDialog::getText(event->widget(),
tr("Edit Text"),tr("Enter new text:"),
QLineEdit::Normal,myText);
if(!text.isEmpty())
{
setText(text);
}
}
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
if(change==ItemPositionHasChanged)
{
qDebug() <<"\nItemPositionHasChanged";//控制台无输出
foreach (Link *link, myLinks)
link->trackNodes();
} return QGraphicsItem::itemChange(change,value);
}
QRectF Node::outlineRect()const
{
const int Padding=10;
QFontMetricsF metrics(qApp->font());
QRectF rect=metrics.boundingRect(myText);
rect.adjust(-Padding,-Padding,+Padding,+Padding);
rect.translate(-rect.center());
return rect;
}
int Node::roundness(double size) const
{
const int Diameter=12;
return 100*Diameter/int(size);
}
------解决思路----------------------
多看看Manual
The item's position has changed. This notification is sent if the ItemSendsGeometryChanges flag is enabled, and after the item's local position, relative to its parent, has changed. The value argument is the new position (the same as pos()), and QGraphicsItem ignores the return value for this notification (i.e., a read-only notification).