Limit Qstring in QrectF
-
hi, i am drawing rectangle on an item and i want to draw text inside the rectangle, but the text is coming out of the box, how can i put my text inside the rectangle(inside bounding rect of item) and below is the code .
@void CBirdsEyeItem::paint(QPainter* f_painter, const QStyleOptionGraphicsItem* f_styleItem, QWidget* f_widget)
{
Q_UNUSED(f_widget);
IBaseReferenceDataItem::paint(f_painter,f_styleItem, f_widget); bq. here i am drawing rectngleconst qreal l_lod = f_styleItem->levelOfDetailFromTransform(f_painter->worldTransform());
if(getPolygon().size() > 4)
{
QPointF l_idPnt = getPolygon().boundingRect().center(); bq. this will give center point of my itemif(l_lod > 5.0)
{
QFont font("Times", 1);
font.setStyleStrategy(QFont::ForceOutline);
f_painter->setFont(font);
f_painter->drawText(l_idPnt, getId());
}
}
}@ -
Set the flag QGraphicsItem::ItemClipsToShape for your item.
-
In this case, you should go with Rahul's code provided above. Make the item large enough to fit the text.
-
bq. how can i put my text inside the rectangle(inside bounding rect of item)
Well, Obviously you need to resize it. It is your need and logic whether you'll need to wrap the text Or adjust the bounding rect Or Font Size.
Refer "Char-map example":http://qt-project.org/doc/qt-5.0/qtwidgets/widgets-charactermap.html
and this old "thread":http://qt-project.org/forums/viewthread/11674 as well