I have a TextItem
inheriting QGraphicsTextItem
. I am trying to get it to work based on specific requirements (center of transformations being center of bounding rectangle).
Since using QGraphicsTextItem::paint()
gives me a little trouble (see this question) and also because I will need special wrapping possibly, it would be ideal to draw text in paint
.
#include <QApplication>
#include <QPainter>
#include <QGraphicsItem>
#include <QGraphicsView>
class TextItem: public QGraphicsTextItem
{
public:
TextItem()
{
QFont f;
f.setPointSize(50);
f.setBold(true);
f.setFamily("Helvetica");
setFont(f);
setHtml("Café Frapé");
setFlags(QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsFocusable |
QGraphicsItem::ItemIsSelectable);
setTextInteractionFlags(Qt::NoTextInteraction);
}
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
qreal w = QGraphicsTextItem::boundingRect().width();
qreal h = QGraphicsTextItem::boundingRect().height();
QRectF r = QRectF(-w/2, -h/2, w, h);
painter->setPen(QPen(m_penColor));
painter->setFont(this->font());
painter->setBrush(Qt::NoBrush);
painter->drawText(r, this->toPlainText(), this->document()->defaultTextOption());
}
QRectF boundingRect() const
{
qreal w = QGraphicsTextItem::boundingRect().width();
qreal h = QGraphicsTextItem::boundingRect().height();
QRectF r = QRectF(-w/2, -h/2, w, h);
return r;
}
protected:
virtual void focusOutEvent (QFocusEvent * event)
{
Q_UNUSED(event);
setTextInteractionFlags(Qt::NoTextInteraction);
}
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)
{
Q_UNUSED(event);
setTextInteractionFlags(Qt::TextEditorInteraction);
setFocus();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TextItem* t = new TextItem();
QGraphicsView view(new QGraphicsScene(-200, -150, 400, 300) );
view.scene()->addItem(t);
view.show();
return a.exec();
}
This works in drawing text with desired options, but I am unable to edit properly - the text cursor is always at the beginning of the text block.
How can I move text cursor ? Is it possible to attach it to the paint ?