Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QGraphicsItem返回不可能的pos()值_Qt_Qt4_Qgraphicsscene_Qt4.8 - Fatal编程技术网

Qt QGraphicsItem返回不可能的pos()值

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&

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<QGraphicsRectItem*>(child)->rect()<<child->scene()->sceneRect()<<child->scene()->views()[0]->sceneRect();
有一些不可能的值:

  • 场景左上角={0,0},但矩形的位置为负
  • QGraphicsRectiItem()的负面结果::rect()::height()
  • QGraphicsItem::SceneboundingDirect()::topLeft()的结果!=QGraphicsItem::scenePos()的结果
  • sceneboundingdirect()返回负值
  • 最小示例

    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);
    }