Qt-interest Archive, March 2007
qcanvas -> qgraphics view
Message 1 in thread
In my transition from QCanvas to QGraphicsView, I have hit one bit of
functionality, I cant find a suitable replacement for.
For the text item, I have two choices of how to replace them, the simple
text, or complex text. However, neighther allows me to set the text
alignment.
I would like to be able to center, justify etc inside the rectangle I am
specifying. I know I can compute the "correct' rectangle, but it's a
lot easier if I don't have to do that.
Thanks in advance
Scott
Message 2 in thread
Scott Aron Bloom wrote:
> In my transition from QCanvas to QGraphicsView, I have hit one bit of
> functionality, I cant find a suitable replacement for.
>
> For the text item, I have two choices of how to replace them, the simple
> text, or complex text. However, neighther allows me to set the text
> alignment.
>
> I would like to be able to center, justify etc inside the rectangle I am
> specifying. I know I can compute the "correct' rectangle, but it's a
> lot easier if I don't have to do that.
>
> Thanks in advance
Interestingly, I had to address that very same problem yesterday! I simply
wanted to create a graphics item that would contain centered text data,
even as the text and other attributes were modified. I could find no way
to do this with either QGraphicsTextItem or QGraphicsSimpleTextItem.
I simply sub-classed QGraphicsItem to create my own text item. In the paint
function, you can justify text any way you want. I keep the text and other
attributes in private variables, and then repaint as needed. The hard part
is to keep up with the bounding rectangle as the attributes change.
QFontMetrics makes that easy.
Here is a snippet from the class definition:
#include <QGraphicsItem>
#include <QGraphicsScene>
class myTextItem : public QObject, public QGraphicsItem {
Q_OBJECT
//=======================================================================
public:
//-----------------------------------------------------------------------
myTextItem( QGraphicsItem* parent=0,
QGraphicsScene* scene=0 );
//-----------------------------------------------------------------------
QRectF boundingRect() const { return Rect; }
...
//-----------------------------------------------------------------------
void paint( QPainter* painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget);
...
//=======================================================================
private:
//-----------------------------------------------------------------------
QRectF Rect;
QString Text;
QFont Font;
...
//=======================================================================
};
...and the significant part of the implementation:
/************************************************************************
************************************************************************
* paint
************************************************************************
************************************************************************/
void myTextItem::paint(QPainter* painter,
const QStyleOptionGraphicsItem*,
QWidget*)
{
//=======================================================================
painter->setFont( ... );
painter->setPen( ... );
...
painter->drawText( Rect, Qt::AlignCenter, Text);
//=======================================================================
}
/************************************************************************
************************************************************************
* minWidth - return minimum width of text
************************************************************************
************************************************************************/
int myTextItem::minWidth() const
{
QFontMetrics fm(Font);
return fm.width(Text);
}
/************************************************************************
************************************************************************
* minHeight - return minimum height of text
************************************************************************
************************************************************************/
int myTextItem::minHeight() const
{
QFontMetrics fm(Font);
return fm.height();
}
--
[ signature omitted ]
Message 3 in thread
Thanks... I was trying to avoid just that :(
In my view, the advanced text item, should take in a text format... Ill
make that recommendation to the trolls :)
Scott
> -----Original Message-----
> From: Larry Bristol [mailto:larry@xxxxxxxxxxxxxx]
> Sent: Friday, March 23, 2007 5:02 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: qcanvas -> qgraphics view
>
> Scott Aron Bloom wrote:
>
> > In my transition from QCanvas to QGraphicsView, I have hit one bit
of
> > functionality, I cant find a suitable replacement for.
> >
> > For the text item, I have two choices of how to replace them, the
simple
> > text, or complex text. However, neighther allows me to set the text
> > alignment.
> >
> > I would like to be able to center, justify etc inside the rectangle
I am
> > specifying. I know I can compute the "correct' rectangle, but it's
a
> > lot easier if I don't have to do that.
> >
> > Thanks in advance
>
> Interestingly, I had to address that very same problem yesterday! I
> simply
> wanted to create a graphics item that would contain centered text
data,
> even as the text and other attributes were modified. I could find no
way
> to do this with either QGraphicsTextItem or QGraphicsSimpleTextItem.
>
> I simply sub-classed QGraphicsItem to create my own text item. In the
> paint
> function, you can justify text any way you want. I keep the text and
> other
> attributes in private variables, and then repaint as needed. The hard
> part
> is to keep up with the bounding rectangle as the attributes change.
> QFontMetrics makes that easy.
>
> Here is a snippet from the class definition:
>
> #include <QGraphicsItem>
> #include <QGraphicsScene>
>
> class myTextItem : public QObject, public QGraphicsItem {
> Q_OBJECT
>
//======================================================================
=
> public:
>
//----------------------------------------------------------------------
-
> myTextItem( QGraphicsItem* parent=0,
> QGraphicsScene* scene=0 );
>
//----------------------------------------------------------------------
-
> QRectF boundingRect() const { return Rect;
}
> ...
>
//----------------------------------------------------------------------
-
> void paint( QPainter* painter,
> const QStyleOptionGraphicsItem
*option,
> QWidget *widget);
> ...
>
//======================================================================
=
> private:
>
//----------------------------------------------------------------------
-
> QRectF Rect;
> QString Text;
> QFont Font;
> ...
>
//======================================================================
=
> };
>
> ...and the significant part of the implementation:
>
>
>
/***********************************************************************
*
>
************************************************************************
> * paint
>
************************************************************************
>
************************************************************************
/
> void myTextItem::paint(QPainter* painter,
> const QStyleOptionGraphicsItem*,
> QWidget*)
> {
>
//======================================================================
=
> painter->setFont( ... );
> painter->setPen( ... );
> ...
> painter->drawText( Rect, Qt::AlignCenter, Text);
>
//======================================================================
=
> }
>
>
/***********************************************************************
*
>
************************************************************************
> * minWidth - return minimum width of text
>
************************************************************************
>
************************************************************************
/
> int myTextItem::minWidth() const
> {
> QFontMetrics fm(Font);
> return fm.width(Text);
> }
>
>
/***********************************************************************
*
>
************************************************************************
> * minHeight - return minimum height of text
>
************************************************************************
>
************************************************************************
/
> int myTextItem::minHeight() const
> {
> QFontMetrics fm(Font);
> return fm.height();
> }
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]
Message 4 in thread
Scott Aron Bloom wrote:
> Thanks... I was trying to avoid just that :(
> In my view, the advanced text item, should take in a text format... Ill
> make that recommendation to the trolls :)
> Scott
You can add my voice to that recommendation! QGraphicsTextItem should
provide support for ordinary text attributes such as this. I figure it
will eventually be included by TT, but I wanted to move on and was not
willing to wait. I find myself subclassing more and more as I go. :-)
--
[ signature omitted ]