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.
Q_OBJECT
public:
enum { Type = UserType + 2 };
~TextEdit();
int type() const { return Type; }
void setGeometry
(const QRectF &rect
);
};
class TextEdit : public QGraphicsTextItem, public QGraphicsLayoutItem {
Q_OBJECT
public:
enum { Type = UserType + 2 };
TextEdit(const QString &text = QString(), QGraphicsItem *parent = 0);
~TextEdit();
int type() const { return Type; }
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
void setGeometry(const QRectF &rect);
};
To copy to clipboard, switch view to plain text mode
#include "rTextEdit.h"
#include <QtGui>
{
}
TextEdit::~TextEdit()
{
}
QSizeF TextEdit
::sizeHint(Qt
::SizeHint which,
const QSizeF &constraint
) const {
switch (which) {
case Qt::MinimumSize:
case Qt::PreferredSize:
return document()->size();
case Qt::MaximumSize:
return QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX
);
default:
qWarning("r::TextEdit::sizeHint(): Don't know how to handle the value of 'which'");
break;
}
return constraint;
}
void TextEdit
::setGeometry(const QRectF &rect
) {
setTextWidth(rect.width());
setPos(rect.topLeft());
}
#include "rTextEdit.h"
#include <QtGui>
TextEdit::TextEdit(const QString &text, QGraphicsItem *parent)
: QGraphicsTextItem(text, parent), QGraphicsLayoutItem(0, false)
{
}
TextEdit::~TextEdit()
{
}
QSizeF TextEdit::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
switch (which) {
case Qt::MinimumSize:
return QSizeF(0, 0);
case Qt::PreferredSize:
return document()->size();
case Qt::MaximumSize:
return QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
default:
qWarning("r::TextEdit::sizeHint(): Don't know how to handle the value of 'which'");
break;
}
return constraint;
}
void TextEdit::setGeometry(const QRectF &rect)
{
setTextWidth(rect.width());
setPos(rect.topLeft());
}
To copy to clipboard, switch view to plain text mode
Bookmarks