Вопрос

I created a class, inherited from QGraphicsTextItem. Object of this class must be movable and must send signal when MouseButton is up.

class MyTextItem: public QObject, public QGraphicsTextItem
{
    Q_OBJECT
public:
    MyTextItem(QObject* Object, QString str1): QGraphicsTextItem(str1), mRef(Object){}
    virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent* event);
    QObject* mRef;
signals:
    void sendSignal(int x, int y);

Then I create object:

MyTextItem* label = new MyTextItem(NULL, "QwertyuiopAsdfghjkl");
label->setPos(p);
label->setFlag(QGraphicsItem::ItemIsMovable, true);

And all is OK. But, when I add:

QFont f;
f.setBold(false);
f.setItalic(false);
f.setPixelSize(16);
f.setFamily("Arial");
f.setLetterSpacing(QFont::AbsoluteSpacing, 0.1);
label->setFont(f);
//
scene()->addItem(label);

My test became cutted off (font is bigger, but width of object is without changes)! Why?. When I use QGraphicsTextItem instead of MyTextItem all is fine.

How to update item size after font increasing?
Thank you!

enter image description here enter image description here

Это было полезно?

Решение

Wow! A wrote

class PolygonLabel: public QGraphicsTextItem, public QObject

instead of

class PolygonLabel: public QObject, public QGraphicsTextItem

and problem was done!

Другие советы

To start with, rather than the multiple inhertance, inherit from QGraphicsObject. The QGraphicsObject class provides a base class for all graphics items that require signals, slots and properties.

The reason your text is cut off may also be due to the fact that you've not overridden the boundingRect function, which returns the bounding rectangle of the area you're drawing in. See here.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top