Results 1 to 3 of 3

Thread: Subclassing QGraphicsTextItem and QGraphicsLayoutItem

  1. #1
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Subclassing QGraphicsTextItem and QGraphicsLayoutItem

    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 

  2. #2
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Subclassing QGraphicsTextItem and QGraphicsLayoutItem

    Ok Trolltech confirmed that this seems to be a bug. A task has been opened: ID 228878

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclassing QGraphicsTextItem and QGraphicsLayoutItem

    you can draw && edit text image on top QGraphicsRectItem.
    It run....

    QGraphicsRectItem can resize rotate any direction ....

    Qt Code:
    1. class TextMount; /* virtual QTextControl like QTextEdit */
    2. class TextLayer : public QObject, public QGraphicsRectItem
    To copy to clipboard, switch view to plain text mode 

    http://code.google.com/p/fop-miniscribus/
    or
    http://fop-miniscribus.googlecode.co...nk/QTextPanel/

    or like paper print on QAbstractScrollArea
    http://fop-miniscribus.googlecode.co.../draw_paper.1/

    or try QGraphicsSimpleTextItem if only words

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.