Limit size of QGraphicsTextItem?



  • Is there a way to limit the size (x & y) of a QGraphicsTextItem?

    If the Content/Document is lager than the size of the TextItem it should be simply stripped.



  • QGraphicsTextItem automatically adjusts size, so it'll never be smaller than document.
    You can get the size through boundingRect(), and than you can make necessary adjustments to string that you are showing.



  • Are there any known alternative Classes that one can use instead of QGraphicsTextItem?
    Adjusting the string isn't really an option.



  • The string is never adjusted, but only the bounding rectangle. As "Jake007" said, override "QGraphicsTextItem::boundingRect":http://developer.qt.nokia.com/doc/qt-4.8/qgraphicstextitem.html#boundingRect and return any size that you want.
    Also if you are dealing with a simple text, "QGraphicsSimpleTextItem":http://developer.qt.nokia.com/doc/qt-4.8/qgraphicssimpletextitem.html is more appropriate



  • Do you mean i should create a new class witch inherits QGraphicsTextItem and write my own boundingRect() witch returns the values i want?



  • [quote author="Artemus" date="1327924598"]Do you mean i should create a new class witch inherits QGraphicsTextItem and write my own boundingRect() witch returns the values i want?[/quote]

    Yes, if you want to limit the bounding rectangle (size).



  • I tried that today and it works, thank you!

    @class QGraphicsTextItemSized : public QGraphicsTextItem
    {
    public:
    QGraphicsTextItemSized(QGraphicsItem* parent = 0);
    QGraphicsTextItemSized(const QString& text, QGraphicsItem* parent = 0);

    QRectF boundingRect() const;
    void   forcePos(qreal x1, qreal y1, qreal x2, qreal y2);
    

    private:
    QRectF forcedSize;
    };@
    @QGraphicsTextItemSized::QGraphicsTextItemSized(QGraphicsItem* parent)
    : QGraphicsTextItem(parent)
    {}

    QGraphicsTextItemSized::QGraphicsTextItemSized(const QString& text, QGraphicsItem* parent)
    : QGraphicsTextItem(text, parent)
    {}

    QRectF QGraphicsTextItemSized::boundingRect() const
    {
    return forcedSize;
    }

    void QGraphicsTextItemSized::forcePos(qreal x1, qreal y1, qreal x2, qreal y2)
    {
    forcedSize.setCoords(0,0,x2-x1,y2-y1);
    setPos(x1,y1);
    }
    @
    @ QString teststring = "Test \n (.Y.) \n Test";
    QGraphicsTextItemSized* text = new QGraphicsTextItemSized();
    scene->addItem(text);
    text->setPlainText(teststring);
    text->forcePos(5,30,115,115);
    text->setTextInteractionFlags(Qt::TextEditorInteraction);@



  • In the above code if i change the boundingrect coordinates to small values total text is not visibling ....can some one please tell me how to adjust the total text with in the bounding rectcordinates....



  • do you added the Constructors?

    @QGraphicsTextItemSized::QGraphicsTextItemSized(QGraphicsItem* parent)
    : QGraphicsTextItem(parent)
    {}

    QGraphicsTextItemSized::QGraphicsTextItemSized(const QString& text, QGraphicsItem* parent)
    : QGraphicsTextItem(text, parent)
    {}@



  • yes i added..i want to fix it exactly with in the bounding rect...that is boundingrect width will be equal to textwidth...i tried like this...

    @

    GraphicsTextItemSized::GraphicsTextItemSized(QGraphicsItem* parent): QGraphicsTextItem(parent){}

    GraphicsTextItemSized::GraphicsTextItemSized(const QString &text, QGraphicsItem *parent):QGraphicsTextItem(text,parent)
    {
    setPlainText( text);
    }
    QRectF GraphicsTextItemSized::boundingRect() const
    {
    return (adjustedrect);
    }

    void GraphicsTextItemSized::forcedsize(qreal x1,qreal y1,qreal x2,qreal y2)
    {
    adjustedrect.setCoords(0,0,x2-x1,y2-y1);
    }

    @

    @
    QGraphicsRectItem *rect = new QGraphicsRectItem(0,0,0,0);
    GraphicsTextItemSized *test = new GraphicsTextItemSized(QString("vcc"),rect);
    test->forcedsize(0,0,10,10);
    test->setPos(10.0,10.0);
    scene->addItem(rect);
    @



  • [quote author="mahesh438" date="1328327702"] @(QStrin("vcc"),rect);@ [/quote]

    -> QStrin g ("vcc") ?

    I can't see any other mistake.



  • i got one doubt wheather we have to use any QFontmatrics to adjust the size of the string to fit in the bounding rect


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.