C++ (Qt)class Text : public QGraphicsTextItem{ Q_OBJECT signals: void inFocus(bool);public: Text() { }protected: void focusOutEvent ( QFocusEvent * event ) { emit inFocus(true); QGraphicsTextItem::focusOutEvent(event); }};
C++ (Qt)class TextItem : public QGraphicsTextItem{ Q_OBJECTprivate: QString oldText; signals: void inFocus(bool); void textChanged();public: TextItem() { }protected: void focusOutEvent ( QFocusEvent * event ) { emit inFocus(false); if(oldText != QGraphicsTextItem::toPlainText()) emit textChanged(); QGraphicsTextItem::focusOutEvent(event); } void focusInEvent ( QFocusEvent * event ) { oldText = QGraphicsTextItem::toPlainText(); emit inFocus(false); QGraphicsTextItem::focusInEvent(event); } void keyPressEvent ( QKeyEvent * event ) { switch (event->key()) { case Qt::Key_Return: if(event->modifiers() & Qt::ControlModifier) scene()->clearFocus(); else QGraphicsTextItem::keyPressEvent(event); break; default:QGraphicsTextItem::keyPressEvent(event); break; } }};
C++ (Qt)QMetaObject::invokeMethod( m_pTextWatcher, "textEditFinished" );