【问题标题】:positioning QGraphicsSvgItem's center on a position.定位 QGraphicsSvgItem 以某个位置为中心。
【发布时间】:2014-09-29 23:20:45
【问题描述】:

我在大小为 48x48 的 QGraphicsScene 上创建了一个 QGraphicsSvgItem。 我想将它的中心设置在 QGraphicsScene 的 x= 50 和 y= 50 上(默认情况下 QGraphicsScene 位于 svg 项目的左上角)。 我继承了 QGraphicsSvgItem 并重新实现了它的绘制函数,然后在 x= -24 y = -24 处应用了翻译操作,但是它显示了 svg 项的 1/4 右下角可见。

所以请建议,如何将 svgitem 的中心对齐在 x=50 ,y =50

【问题讨论】:

    标签: c++ qt svg


    【解决方案1】:

    默认情况下,QGraphicsItem 的局部左上角为 (0,0)。但是,如果您从 QGraphicsItem 继承,您可以更改它,使 (0,0) 成为项目的中心。

    这由boundingRect() 函数处理。如果您有一个高度和宽度为 50 的项目,则默认边界矩形将为 (x,y,w,h) 返回 (0,0,50,50)。

    让局部中心(0,0)覆盖boundingRect函数返回(-25,-25,50,50)。

    QRectF boundingRect() const
    {
        return QRectF(-25, -25, 50, 50);
    }
    

    然后,要将场景中的项目居中在 (50,50),您只需将项目的位置设置为该坐标

    item->setPos(50, 50);
    

    【讨论】:

      【解决方案2】:

      还需要重新实现QGraphicsSvgItem的paint方法

      QRectF boundingRect() const
      {
              return QRectF(-24,-24,48,48);
      }
      
      
      void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
      {
          painter->translate(-24,-24);
          QGraphicsSvgItem::paint(painter,option,widget);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多