Hi

I'm trying to subclass these 2 classes in order to get an editable text element which I can lay out on QGraphicsWidget. The code for the resulting class is shown below. The problem is that when I add my subclass instance to any QGraphicsLayout - it just crashes. The stack trace indicates a problem in the QGraphicsItem::setParent() function.

I created a similar subclass from QAbstractGraphicsShapeItem and QGraphicsLayoutItem, and it works fine - no crashing.


Qt Code:
  1. class TextEdit : public QGraphicsTextItem, public QGraphicsLayoutItem {
  2. Q_OBJECT
  3. public:
  4. enum { Type = UserType + 2 };
  5.  
  6. TextEdit(const QString &text = QString(), QGraphicsItem *parent = 0);
  7. ~TextEdit();
  8.  
  9. int type() const { return Type; }
  10.  
  11. QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
  12. void setGeometry(const QRectF &rect);
  13. };
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "rTextEdit.h"
  2.  
  3.  
  4. #include <QtGui>
  5.  
  6.  
  7. TextEdit::TextEdit(const QString &text, QGraphicsItem *parent)
  8. : QGraphicsTextItem(text, parent), QGraphicsLayoutItem(0, false)
  9. {
  10.  
  11. }
  12.  
  13. TextEdit::~TextEdit()
  14. {
  15. }
  16. QSizeF TextEdit::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
  17. {
  18. switch (which) {
  19. case Qt::MinimumSize:
  20. return QSizeF(0, 0);
  21. case Qt::PreferredSize:
  22. return document()->size();
  23. case Qt::MaximumSize:
  24. return QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
  25. default:
  26. qWarning("r::TextEdit::sizeHint(): Don't know how to handle the value of 'which'");
  27. break;
  28. }
  29. return constraint;
  30. }
  31.  
  32. void TextEdit::setGeometry(const QRectF &rect)
  33. {
  34. setTextWidth(rect.width());
  35. setPos(rect.topLeft());
  36. }
To copy to clipboard, switch view to plain text mode