Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2007
QGraphicsScene query


Message 1 in thread

Hi,
 
I am adding a QGraphicsRectItem in QGraphicsScene. That Rectangle inturn
contains a QGraphicsTextItem member.
I create QGraphicsTextItem as a child of QGraphicsRectItem.
 
Still, the text is not bounded by the client area of the parent. 
The text follows the co-ordinate system of the scene and not of its
parent.
 
What I really want is a text to be displayed inside the rectangle and
the text to move relative to the rectangle.
Beign from an MFC world, I am used to seeing the child restricting
itself to the client area of the parent.
 
The code snippet is as below:
 
//HEADER FILE
class TextRect : public QGraphicsRectItem
{
QGraphicsTextItem *m_textEdit;
public:
TextRect(const QRectF & rect, QGraphicsItem * parent = 0, QGraphicsScene
* scene = 0);
};
 
 
//CPP FILE
TextRect::TextRect(const QRectF &rect, QGraphicsItem *parent,
QGraphicsScene *scene) : QGraphicsRectItem(rect, parent, scene)
{
m_textEdit = new QGraphicsTextItem("Hello",this);
m_textEdit->setTextInteractionFlags(Qt::TextEditable);
 
}
 
 
//INVOCATION
m_scene = new QGraphicsScene(widget); ;
m_scene->setSceneRect(0,0,800,500);
QGraphicsItem *item = new TextRect(QRectF(20,20,100,100));
m_scene->addItem(item);
m_view = new MyView(m_scene, widget);
m_view->show();
 
 
Pl let me know where am I going wrong ?
 
Thnx
Prateek

Message 2 in thread

Prateek Tiwari wrote:

> I am adding a QGraphicsRectItem in QGraphicsScene. That Rectangle inturn
> contains a QGraphicsTextItem member.
> I create QGraphicsTextItem as a child of QGraphicsRectItem.
>  
> Still, the text is not bounded by the client area of the parent.
> The text follows the co-ordinate system of the scene and not of its
> parent.
>  
> What I really want is a text to be displayed inside the rectangle and
> the text to move relative to the rectangle.
> Beign from an MFC world, I am used to seeing the child restricting
> itself to the client area of the parent.

I'm certainly no expert on this, but I do not see anything wrong with what
you are doing.  It's just that the parentage of a QGraphicsItem does not
work the same way as that of a QWidget.  So your MFC world experience is
not completely applicable in this new world.

The boundingRect of a QGraphicsItem is *not* used for trimming its children
(at least not in Qt 4.2.1) in the same way that children of a QWiget are
trimmed.  It is perfectly normal for the text of a QGraphicsTextItem to
appear outside of the boundingRect of the parent QGraphicsRectItem. 
Indeed, the text can even be *completely* outside of the parent's
boundingRect!  I (now) do this sort of thing all the time in an application
I am working on.  I'll admit it surprised me when I first did this
accidentally, but after a bit of reflection, it began to make sense to me. 
If you want it trimmed by its parent's boundingRect, I think you'll have to
do that bit of magic yourself.

On the other hand, the positions of child items should be relative to their
parent (not to the QGraphicsScene unless they are top level items).  If you
move your QGraphicsRectItem to a new position relative to *its* parent
(remember that the position of a QGraphicsItem is given using its parent's
coordinate system), the children of the rectangle should move right along
with it (because *their* positions are given using their parent's
coordinate system).

Hope this helps!

--
 [ signature omitted ] 

Message 3 in thread

Thnx. That was indeed helpful.

Regards
Prateek 

-----Original Message-----
From: Larry Bristol [mailto:larry@xxxxxxxxxxxxxx] 
Sent: Tuesday, April 10, 2007 6:04 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: QGraphicsScene query

Prateek Tiwari wrote:

> I am adding a QGraphicsRectItem in QGraphicsScene. That Rectangle 
> inturn contains a QGraphicsTextItem member.
> I create QGraphicsTextItem as a child of QGraphicsRectItem.
>  
> Still, the text is not bounded by the client area of the parent.
> The text follows the co-ordinate system of the scene and not of its 
> parent.
>  
> What I really want is a text to be displayed inside the rectangle and 
> the text to move relative to the rectangle.
> Beign from an MFC world, I am used to seeing the child restricting 
> itself to the client area of the parent.

I'm certainly no expert on this, but I do not see anything wrong with
what you are doing.  It's just that the parentage of a QGraphicsItem
does not work the same way as that of a QWidget.  So your MFC world
experience is not completely applicable in this new world.

The boundingRect of a QGraphicsItem is *not* used for trimming its
children (at least not in Qt 4.2.1) in the same way that children of a
QWiget are trimmed.  It is perfectly normal for the text of a
QGraphicsTextItem to appear outside of the boundingRect of the parent
QGraphicsRectItem. 
Indeed, the text can even be *completely* outside of the parent's
boundingRect!  I (now) do this sort of thing all the time in an
application I am working on.  I'll admit it surprised me when I first
did this accidentally, but after a bit of reflection, it began to make
sense to me. 
If you want it trimmed by its parent's boundingRect, I think you'll have
to do that bit of magic yourself.

On the other hand, the positions of child items should be relative to
their parent (not to the QGraphicsScene unless they are top level
items).  If you move your QGraphicsRectItem to a new position relative
to *its* parent (remember that the position of a QGraphicsItem is given
using its parent's coordinate system), the children of the rectangle
should move right along with it (because *their* positions are given
using their parent's coordinate system).

Hope this helps!

--
 [ signature omitted ]