【问题标题】:Change QGraphicsItem's local Co-Ordinate system to make origin at (0,0)更改 QGraphicsItem 的本地坐标系统,使其原点位于 (0,0)
【发布时间】:2013-02-09 19:36:19
【问题描述】:

我有一个 QGraphicsView 和一个名为 scene->setSceneRect(0,0,600, 600) 的 QGraphicsScene。

我用 boundingRectangle(0,0,600,600) 创建了一个名为 background 的简单自定义 QGraphicsItem。

非常清楚背景项目的中心是 (300,300),minX=0, minY=0 和 maxX=600, maxY=600...但我希望这个背景项目的中心是 (0,0) minX=-300,minY=-300,原点在(0,0),maxX=300,maxY=300。 换句话说,我希望背景项目的本地坐标系能够反映我们在纸上绘制的自然坐标系。


(来源:shmoop.com

我该怎么做。

【问题讨论】:

    标签: qt qt4 qt5 qgraphicsitem qgraphicsscene


    【解决方案1】:

    如果您有自定义的QGraphcisItem,您将负责绘画和几何图形。因此,您可以将矩形绘制为左上角 (-300,-300) 和右下角 (300,300),只要确保通过覆盖和实现 QGraphicsItem::boundingRect() 返回匹配的边界矩形。

    这是 Qt 文档中的一个示例:

     class SimpleItem : public QGraphicsItem
     {
     public:
         QRectF boundingRect() const
         {
             qreal penWidth = 1;
             return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                           20 + penWidth, 20 + penWidth);
         }
    
         void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                    QWidget *widget)
         {
             painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
         }
     };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      相关资源
      最近更新 更多