PDA

View Full Version : How draw extra border on QGraphicsTextItem?



patrik08
8th July 2007, 11:26
/*
incomming QDomElement
<divtext rotate="90" zindex="2" left="115" top="25" large="300" hight="400" >
<param font="arial" color="#0000ff" size="30">Scribble XHTML Floating DIV box width:300px height:400px top:25px left:115px</param>
</divtext>
*/

class FloatText : public QGraphicsTextItem
{
Q_OBJECT
public:
FloatText( QDomElement xmlstream , QGraphicsItem *parent, QGraphicsScene *scene )
: QGraphicsTextItem(parent, scene)
{
modus = MODUS_MOVE;
root = xmlstream;
moveBy(GetInt("left"),GetInt("top"));
rotate(GetInt("rotate"));
f = QFont();
int FontSize = root.firstChildElement("param").attributeNode("size").value().toInt();
if (FontSize > 9) {
f.setPixelSize(FontSize);
} else {
f.setPixelSize(10);
}
f.setFamily( root.firstChildElement("param").attributeNode("font").value() );
setDefaultTextColor ( root.firstChildElement("param").attributeNode("color").value() );
human = root.firstChildElement("param").text();
setTextWidth ( GetInt("large") );
setPlainText ( human );
setFont(f);
setZValue( GetInt("zindex") + 300 ); /* to make border - */
SetMoveItem();
QObject::connect(scene, SIGNAL(selectionChanged()), this, SLOT(WakeUpHere()));
/////// ensureVisible(QRectF(GetInt("left"),GetInt("top"),GetInt("large"),GetInt("hight")),80,80);
}

void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
{
painter->drawRect (-2,-2,GetInt("large") + 4,8); /* GetInt("hight") + 4 border 2px out side overflow*/
update();
}

patrik08
8th July 2007, 12:32
I not make forward paint :o ..

but now ony resize window display a correct border why?



void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
{
QPen pen;
pen.setStyle( Qt::SolidLine );
pen.setWidth( 2 );
pen.setColor( option->palette.highlight().color() );
/////scene()->clearSelection();
////prepareGeometryChange();
painter->setPen( pen);
painter->drawRect (-2,-2,GetInt("large") + 4,GetInt("hight") + 4);
QGraphicsTextItem::paint(painter,option,widget);
}

jpn
8th July 2007, 12:38
All painting must be restricted to inside an item's bounding rect (http://doc.trolltech.com/4.3/qgraphicsitem.html#boundingRect).

patrik08
8th July 2007, 13:33
All painting must be restricted to inside an item's bounding rect (http://doc.trolltech.com/4.3/qgraphicsitem.html#boundingRect).

If i set setTextWidth(300); his paint only on this area ... and text break on this limit correct.
Now i must grab a way to become setTextHight(x) to display text only on this limit, like xhtml div overflow / hidden http://de.selfhtml.org/css/eigenschaften/positionierung.htm#overflow

inside moveBy(10,10); and how i find the way to increase the bounding width whitout destroy textwidht pixel ?

I not find SetboundingRect(X) area ... and margin? to increase this paint area? to make my border and limit text Width x Hight.

Gopala Krishna
8th July 2007, 15:11
You need to override boundingRect() to return enlarged rect. I did some sample code.
This worked for me for all rotations and pixel sizes. Here it is.
Guess this is what you wanted


#include <QtGui>

class FloatText : public QGraphicsTextItem
{
//Q_OBJECT;
public:
FloatText( QGraphicsScene *scene )
: QGraphicsTextItem(0, scene)
{
setPlainText ( "human ");
QFont f = font();
f.setPixelSize(40);
setFont(f);
rotate(-90);
setFlags(QGraphicsItem::ItemIsMovable);
}

QRectF boundingRect() const
{
return QGraphicsTextItem::boundingRect().adjusted(-2,-2,+2,+2);
}

void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
{
QGraphicsTextItem::paint(painter,option,widget);
// Isn't this strange to call update in paint event ?
//update();
painter->setPen(Qt::black);
painter->drawRect(boundingRect());
}

};

int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QGraphicsScene scene(0,0,1024,768);
FloatText t(&scene);
t.setPos(500,500);
QGraphicsView view(&scene);
view.show();
return app.exec();
}