2012-07-12 48 views
7

我有一个QGraphicsTextItem父对QGraphicsItem。我希望QGraphicsTextItem始终位于QGraphicsItem的正上方,但我还希望文本在缩放系数低于1时保持相同大小,即文本保持其缩放系数为1的大小,即使父图形项目缩小。我发现,当比例因子低于1时,将QGraphicsItem::ItemIgnoresTransformations标志设置为true就能保持尺寸。应用QGraphicsItem :: ItemIgnoresTransformations后维护相对的子位置

但我似乎无法找到一种方式来让文本的位置总是保持在QGraphicsItem之上。有没有办法做到这一点?我尝试使用deviceTransform()函数,但是当我滚动出来时,文本仍然从QGraphicsItem移开。更糟糕的是,一些文本项目开始“摇摆”,即他们开始不断地改变自己的位置,以至于看起来像在颤抖。如果这是我需要使用的功能,我想我不知道如何正确使用它。

在我添加了一个QGraphicsTextItem我的QGraphicsItem的构造函数:

fTextItem = new QGraphicsTextItem(getName(), this); 
fTextItem->setFlag(QGraphicsItem::ItemIgnoresTransformations); 

下面是从的QGraphicsItem的涂料功能的代码片段

qreal lod = painter->worldTransform().m22(); 
if(lod <= 1.0) { 
    fTextItem-setFlag(QGraphicsItem::ItemIgnoresTransformations); 
    fTextItem->setPos(fTextItem->deviceTransform(view-viewportTransform()).inverted().map(view->mapFromScene(mapToScene(0,0)))); 
} else { 
    fTextItem->setFlag(QGraphicsItem::ItemIgnoresTransformations, false); 
    fTextItem->setPos(0, 0); 
} 
+0

你不应该设置QGraphicsTextItem对象标志忽略父转型,而不是对的QGraphicsItem? – ArmenB 2017-01-12 18:01:59

回答

4

免责声明:这可能是矫枉过正你正在努力去做。在我们的项目中,我们有一些额外的限制,使得这个解决方案对我们来说最简单。

我们不得不在一个项目中做类似的事情,它最终最容易让我们不使用ItemIgnoresTransformations,而是推出我们自己的转换。以下是我们用于创建仅用于转换(无缩放)转换以在特定位置绘制项目的主要功能。您可能可以根据自己的使用情况对其进行修改。

static QTransform GenerateTranslationOnlyTransform(
    const QTransform &original_transform, 
    const QPointF &target_point) { 
    // To draw the unscaled icons, we desire a transform with scaling factors 
    // of 1 and shearing factors of 0 and the appropriate translation such that 
    // our icon center ends up at the same point. According to the 
    // documentation, QTransform transforms a point in the plane to another 
    // point using the following formulas: 
    // x' = m11*x + m21*y + dx 
    // y' = m22*y + m12*x + dy 
    // 
    // For our new transform, m11 and m22 (scaling) are 1, and m21 and m12 
    // (shearing) are 0. Since we want x' and y' to be the same, we have the 
    // following equations: 
    // m11*x + m21*y + dx = x + dx[new] 
    // m22*y + m12*x + dy = y + dy[new] 
    // 
    // Thus, 
    // dx[new] = m11*x - x + m21*y + dx 
    // dy[new] = m22*y - y + m12*x + dy 
    qreal dx = original_transform.m11() * target_point.x() 
      - target_point.x() 
      + original_transform.m21() * target_point.y() 
      + original_transform.m31(); 
    qreal dy = original_transform.m22() * target_point.y() 
      - target_point.y() 
      + original_transform.m12() * target_point.x() 
      + original_transform.m32(); 

    return QTransform::fromTranslate(dx, dy); 
} 

要使用,采取QPainter变换传递给paint方法和做类似:

painter->save(); 
painter->setTransform(GenerateTranslationOnlyTransform(painter->transform(), 
                 some_point)); 
// Draw your item. 
painter->restore(); 
+0

在这一点上看起来像我的问题矫枉过正:)但如果没有简单的方法坚实的基础。非常感谢! – KD07 2012-07-12 21:58:00

+0

我尝试在我的应用程序中使用此自定义转换。它的工作原理与转变有关,但我的物品位置仍然关闭。我希望我的子项目(具有自定义转换)停留在父级的(0,0)点。我将some_point传递为(0,0)。 – KD07 2012-07-18 21:53:44

6

我的建议是继承QGraphicsSimpleTextItem以这种方式:

class TextItem 
    : public QGraphicsSimpleTextItem 
{ 
public: 
    TextItem(const QString &text) 
     : QGraphicsSimpleTextItem(text) 
    { 

    } 
    void paint(QPainter *painter, 
     const QStyleOptionGraphicsItem *option, QWidget *widget) 
    { 
     painter->translate(boundingRect().topLeft()); 
     QGraphicsSimpleTextItem::paint(painter, option, widget); 
     painter->translate(-boundingRect().topLeft()); 
    } 
    QRectF boundingRect() const 
    { 
     QRectF b = QGraphicsSimpleTextItem::boundingRect(); 
     return QRectF(b.x()-b.width()/2.0, b.y()-b.height()/2.0, 
      b.width(), b.height()); 
    } 
}; 
QGraphicsSimpleTextItem *mText = new TextItem("Item"); 
scene()->addItem(mText); 
mText->setFlag(QGraphicsItem::ItemIgnoresTransformations, true); 
mText->setPos(itemToFollow->pos()); 
+0

请注意,KD07询问了“QGraphicsTextItem”(它支持HTML /富文本格式)。可以在[Qt Center](http://www.qtcentre.org/threads/51168-QGraphicsTextItem-center-based-coordinates)上找到另一个关于“QGraphicsSimpleTextItem”的文章。 – handle 2017-04-19 14:09:56

0

添加到Dave Mateer的答案中,我认为在某些情况下,应该保留适当的边界矩形(以及形状)对象。对于我来说,我需要修改boundingRect()以适当的对象选择行为。请记住,如果我们不使用ItemIgnoresTransformations标志,则该对象的边界矩形将像往常一样缩放和转换。所以我们还需要重新调整boundingRect以保持视图独立效果。

保持独立于视图的边界矩形非常简单:只需从deviceTransform(m_view->viewportTransform()).inverted().m11()中获取比例因子并将此常数乘以本地坐标边界矩形即可。例如:

qreal m = this->deviceTransform(m_view->viewportTransform()).inverted().m11(); 
return QRectF(m*(m_shapeX), m*(m_shapeY), 
       m*(m_shapeR), m*(m_shapeR)); 
0

Dave Mateer很好的回答!我遇到了问题,我想在不同的缩放级别定义不同的比例因子。这是我做的:

void MyGraphicsItem::paint(QPainter * painter, const QStyleOptionGraphicsItem* option, QWidget* widget) 
{ 
    //save painter for later operations 
    painter->save(); 
    QTransform originalTransform = painter->transform(); 
    QPointF originalCenter = rect().center(); 
    qreal dx = originalTransform.m11() * originalCenter.x() + originalTransform.m21() * originalCenter.y() + originalTransform.m31(); 
    qreal dy = originalTransform.m22() * originalCenter.y() + originalTransform.m12() * originalCenter.x() + originalTransform.m32(); 
    //normally our target scale factor is 1, meaning the item has keeps its size, regardless of zoom 
    //we adjust the scale factor though when the item is smaller than one pixel in comparison to the background image 
    qreal factor = 1.0; 
    //check if scale factor if bigger that the item size, and thus it occupies less that a pixel in comparision to the background image 
    if (rect().width() < originalTransform.m11()) { 
     //calculate adjusted scale factor 
     factor = originalTransform.m11()/rect().width(); 
    } 
    //adjust position according to scale factor 
    dx -= factor * originalCenter.x(); 
    dy -= factor * originalCenter.y(); 
    //set the new transform for painting 
    painter->setTransform(QTransform::fromScale(factor, factor) * QTransform::fromTranslate(dx, dy)); 
    //now paint... 
    QGraphicsXYZItem::paint(painter, option, widget); 
    //restore original painter 
    painter->restore(); 
} 

你确实需要太调整边框在这种情况下:

QRectF MyGraphicsItem::boundingRect() const 
{ 
    QRectF rect = QGraphicsEllipseItem::boundingRect(); 
    //this is a bit hackish, let me know if you know another way... 
    if (scene() != NULL && scene()->views().at(0) != NULL) 
    { 
     //get viewport transform 
     QTransform itemTransform = scene()->views().at(0)->transform(); 
     QPointF originalCenter = rect.center(); 
     //calculate back-projected original size of item 
     qreal realSizeX = rect.width()/itemTransform.m11(); 
     qreal realSizeY = rect.height()/itemTransform.m11(); 
     //check if scale factor is bigger that the item size, and thus it occupies less that a pixel in comparison 
     //to the background image and adjust size back to equivalent of 1 pixel 
     realSizeX = realSizeX < 1.0 ? 1.0 : realSizeX; 
     realSizeY = realSizeY < 1.0 ? 1.0 : realSizeY; 
     //set adjusted position and size according to scale factor 
     rect = QRectF(rect.center().x() - realSizeX/2.0, rect.center().y() - realSizeY/2.0, realSizeX, realSizeY); 
    } 
    return rect; 
} 

这种解决方案的项目工作非常好我的情况。

0

这里是一个解决方案,我设计了非常温和的复杂性:

1)获取boundingRect()母公司和其映射到现场 2)取分的这个列表的最小X和Y,这是您的产品真实产地,在现场协调 3)设置孩子

在Pyside的位置:

br = parent.mapToScene(parent.boundingRect()) 
    realX = min([item.x() for item in br]) 
    realY = min([item.y() for item in br]) 
    child.setPos(parent.mapFromScene(realX, realY)) #modify according to need 
0

我找到了另一种解决方案,不涉及任何转换或搞乱由手缩放/定位。有在QGraphicsItem::ItemIgnoresTransformations标志描述一个提示:

QGraphicsItem::ItemIgnoresTransformations

项忽略继承变换(即,其位置 仍锚定在其母体,但父或视图旋转,缩放或 剪切变换被忽略)。 [...]

而这就是关键!我们需要两个项目:一个会保留相对位置(没有设置任何标记)的父项和一个子项目,该父项将在父级(0,0)点(使用QGraphicsItem::ItemIgnoresTransformations标志设置)进行绘图。就那么简单!

我封装这个功能集成到一个单一的类 - 这里是一些代码:

#include <QGraphicsItem> 
#include <QPainter> 

class SampleShape : public QGraphicsItem 
{ 
private: 
    /* This class implements shape drawing */ 
    class SampleShapeImpl : public QGraphicsItem 
    { 
    public: 
     SampleShapeImpl (qreal len, QGraphicsItem *parent = nullptr) 
      : QGraphicsItem(parent), m_len(len) 
     { 
      /* ignore transformations (!) */ 
      setFlag(QGraphicsItem::ItemIgnoresTransformations); 
     } 

     QRectF boundingRect (void) const override 
     { 
      /* sample bounding rectangle */ 
      return QRectF(-m_len, -m_len, m_len*2, m_len*2); 
     } 

     void paint (QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override 
     { 
      /* draw a shape, (0,0) is an anchor */ 
      painter->drawLine(0, -m_len, 0, m_len); 
      painter->drawLine(-m_len, 0, m_len, 0); 
      // ... 
     } 

    private: 
     qreal m_len; // sample shape parameter 
    }; 

public: 
    /* This is actually almost an empty class, you only need to set 
    * a position and pass any parameters to a SampleShapeImpl class. 
    */ 
    SampleShape (qreal x, qreal y, qreal len, QGraphicsItem *parent = nullptr) 
     : QGraphicsItem(parent), m_impl(len, this) // <-- IMPORTANT!!! 
    { 
     /* set position at (x, y), view transformations will apply */ 
     setPos(x, y); 
    } 

    QRectF boundingRect (void) const override 
    { 
     return QRectF(); // it's just a point, no size 
    } 

    void paint (QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override 
    { 
     // empty, drawing is done in SampleShapeImpl 
    } 

private: 
    SampleShapeImpl m_impl; 
};