Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPlainTextEdit in a QGraphicsView



  • Hi,

    I am trying to embed a movable code editor into a QGraphicsView.

    Currently I am able to embed a QPlainTextEdit into a QGraphicsView. I use QGraphicsProxyWidget and also set QPlainTextEdit `s parent as another QGraphicsWidget item.

       QGraphicsWidget* parentWidget = new QGraphicsWidget();
      parentWidget->setMinimumSize(QSizeF(100, 30));
      parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
      parentWidget->setAutoFillBackground(true);
      scene->addItem(parentWidget);
    
     editor = new QPlainTextEdit(); 
     QGraphicsProxyWidget *proxy = scene->addWidget( editor);
     proxy->setParentItem(parentWidget);
     proxy->setWidget(editor);
     proxy->setPos(5, 5);
     proxy->setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemSendsGeometryChanges |
                            QGraphicsItem::ItemSendsScenePositionChanges);
    

    This helps me to render the QPlainTextEdit object in QGraphicsView. It also receives mouse events and it is movable since its QGraphicsWidget parent is movable.

    The problem is QPlainTextEdit object does not receive keyboard events. How can I achieve this by using this setup ?

    If I can not , which one would be the better alternative ?

    a) Implementing a code editor from QGraphicsTextItem
    b) Rendering QPlainTextEdit on top of QGraphicsWidget and making it movable

    Thanks in advance


  • Lifetime Qt Champion

    Hi,

    Looks like you are missing the QGraphicsItem::ItemIsFocusable flag.


  • Lifetime Qt Champion

    Hi,

    Looks like you are missing the QGraphicsItem::ItemIsFocusable flag.



  • Thank you very much , it worked !


Log in to reply