PDA

View Full Version : QGraphicsRectItem with text



ttvo
26th August 2009, 20:40
I subclass QGraphicsRectItem in order to add a text inside the rectangle.
1) The text doesn't get re-paint properly. When I created it as


RectItem *myRect = new JFIRectItem("A Very Long Long Word");
addItem(myRect);

it shows up on the QGraphicsScene fine, but the text is missing when I have it in a dropEvent


RectItem *item = new RectItem(m_selectedJFIName.c_str());
item->setPos(mouseEvent->scenePos());
addItem(item);

2) How am I going to wrap the text so that it will fit inside the rectangle and to also re-size the rectangle as needed
3609



class RectItem : public QObject, public QGraphicsRectItem
{
Q_OBJECT
public:
RectItem (std::string text=0, QGraphicsItem *parent = 0);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget = 0);

private:
std::string m_text;
};

RectItem::RectItem (std::string text, QGraphicsItem *parent)
: QGraphicsRectItem(100, 100, 100, 30, parent)
{
m_text = text;
}

void RectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
QPointF pos = QPointF(rect().x() + 10, rect().y() + 20);
painter->drawText(pos, m_text.c_str());
painter->setPen(QPen(Qt::black, 1));
painter->drawRoundRect(rect());
}


Thanks.

Lykurg
26th August 2009, 20:53
You have to deal with QTextDocument, but it might be easier to subclass QGraphicsTextItem and draw the border...