Qt QGraphicsItem返回不可能的pos()值
Qt4.8Linux 使用鼠标移动QGraphicsRectItem时,项目可视地保留在场景中:Qt QGraphicsItem返回不可能的pos()值,qt,qt4,qgraphicsscene,qt4.8,Qt,Qt4,Qgraphicsscene,Qt4.8,Qt4.8Linux 使用鼠标移动QGraphicsRectItem时,项目可视地保留在场景中: auto new_bnd_rect = child->sceneBoundingRect(); qDebug()<<new_bnd_rect.topLeft()<<child->scenePos()<<child->pos(); qDebug()<<new_bnd_rect; qDebug()<<dynamic_cast&
auto new_bnd_rect = child->sceneBoundingRect();
qDebug()<<new_bnd_rect.topLeft()<<child->scenePos()<<child->pos();
qDebug()<<new_bnd_rect;
qDebug()<<dynamic_cast<QGraphicsRectItem*>(child)->rect()<<child->scene()->sceneRect()<<child->scene()->views()[0]->sceneRect();
有一些不可能的值:
class ControlledRect : public QObject, public QGraphicsRectItem
{
Q_OBJECT
public:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
signals:
void moved_to(QPointF);
};
QVariant ControlledRect::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPointF newPos = value.toPointF();
emit moved_to(newPos);
}
return QGraphicsItem::itemChange(change, value);
}
class Control : public QObject
{
Q_OBJECT
ControlledRect* child;
public slots:
void send_rect_changed(QPointF new_pos);
};
void Control::send_rect_changed(QPointF new_pos)
{
auto new_bnd_rect = child->sceneBoundingRect();
qDebug()<<new_bnd_rect.topLeft()<<child->scenePos()<<child->pos();
qDebug()<<new_bnd_rect;
qDebug()<<dynamic_cast<QGraphicsRectItem*>(child)->rect()<<child->scene()->sceneRect()<<child->scene()->views()[0]->sceneRect();
}
Control::Control()
{
...
connect(child, SIGNAL(moved_to(QPointF)), this, SLOT(send_rect_changed(QPointF)), Qt::QueuedConnection);
}
类受控项:公共QObject、公共QGraphicsRecItem
{
Q_对象
公众:
QVariant项目变更(图形站点变更、常量QVariant和值);
信号:
无效移动到(QPointF);
};
QVariant ControlledRect::itemChange(QGraphicsItem::GraphicsItemChange,常量QVariant&value)
{
if(change==ItemPositionChange&&scene()){
//值是新的位置。
QPointF newPos=value.toPointF();
发射移动到(新位置);
}
返回QGraphicsItem::itemChange(change,value);
}
类控件:公共QObject
{
Q_对象
ControlledRect*儿童;
公众时段:
无效发送更改(QPointF new pos);
};
无效控制::发送更改(QPointF new\u pos)
{
auto new_bnd_rect=child->sceneboundingdirect();
qDebug()请提供一个“我看不出什么是不可能的”。根据您的具体操作,场景的直线可能会自动调整,以确保QGraphicsRectItem
保持可见。项目的位置如何!=项目的位置?child->sceneboundingdirect().topLeft();返回QPointF(-1.5,-1.5)同时,child->scenePos()返回QPointF(-142.597,-208.512)
class ControlledRect : public QObject, public QGraphicsRectItem
{
Q_OBJECT
public:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
signals:
void moved_to(QPointF);
};
QVariant ControlledRect::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPointF newPos = value.toPointF();
emit moved_to(newPos);
}
return QGraphicsItem::itemChange(change, value);
}
class Control : public QObject
{
Q_OBJECT
ControlledRect* child;
public slots:
void send_rect_changed(QPointF new_pos);
};
void Control::send_rect_changed(QPointF new_pos)
{
auto new_bnd_rect = child->sceneBoundingRect();
qDebug()<<new_bnd_rect.topLeft()<<child->scenePos()<<child->pos();
qDebug()<<new_bnd_rect;
qDebug()<<dynamic_cast<QGraphicsRectItem*>(child)->rect()<<child->scene()->sceneRect()<<child->scene()->views()[0]->sceneRect();
}
Control::Control()
{
...
connect(child, SIGNAL(moved_to(QPointF)), this, SLOT(send_rect_changed(QPointF)), Qt::QueuedConnection);
}