[Qt-interest] setTextInteractionFlags in QGraphicsTextItem does nothing

Darien Alonso Camacho darienad030111 at vcl.jovenclub.cu
Thu Apr 7 22:47:51 CEST 2011


Hi all.
I'm trying to wrap a QGraphicsTextItem into a class that inherits 
QGraphicsItem too, doing this way to adjust the class to the design I 
want. On the QGraphicsScene::keyPress(...) event I add the TextItem 
object and do text->setTextInteractionFlags( Qt::TextEditorInteraction 
); the text item is added to the scene well but the problem is the text 
item do not shows the cursor, just shows an empty item.

Am I doing something wrong??
Any advice??

Best regards.

Here is part of the code:

//This is the parent for all graphics items
class GraphicItem : public QGraphicsItem
{

public:
    GraphicItem();
    //Reimplemented from QGraphicsItem
    virtual QRectF boundingRect() const {}
    virtual QPainterPath shape() const {}
    virtual void paint( QPainter* painter, const 
QStyleOptionGraphicsItem* option, QWidget* widget = 0 ) {}
    ...
};

//This is my text item class
class TextItem : public QObject, public GraphicItem
{
    Q_OBJECT

public:
    TextItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0)
    {
        setFlag( QGraphicsItem::ItemIsMovable );
        setFlag( QGraphicsItem::ItemIsSelectable );
        setFlag( QGraphicsItem::ItemIsFocusable );
        text = new QGraphicsTextItem();
    }
    QRectF boundingRect() const
    {
        return text->boundingRect();
    }
    QPainterPath shape() const
    {
        return text->shape();
    }
    void paint( QPainter* painter, const QStyleOptionGraphicsItem* 
option, QWidget* widget = 0 )
    {
        text->paint( painter, option, widget );
    }

signals:
    void lostFocus(TextItem *item);
    void selectedChange(QGraphicsItem *item);

protected:
    QVariant itemChange(GraphicsItemChange change, const QVariant &value)
    {
        if ( change == QGraphicsItem::ItemSelectedHasChanged ){
            emit selectedChange( this );
    }

    }
    void focusOutEvent( QFocusEvent* event )
    {
        text->setTextInteractionFlags( Qt::NoTextInteraction );
             emit lostFocus( this );
    }

    void mouseDoubleClickEvent( QGraphicsSceneMouseEvent* event )
    {
        if ( text->textInteractionFlags() == Qt::NoTextInteraction )
        {
            text->setTextInteractionFlags( Qt::TextEditorInteraction );
        }
    }

private:
    QGraphicsTextItem* text;
};





More information about the Qt-interest-old mailing list