Results 1 to 5 of 5

Thread: Problem with QGraphicsTextItem

  1. #1
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs down Problem with QGraphicsTextItem

    I have a class
    Qt Code:
    1. class Letter : public QGraphicsTextItem
    2. {
    3. public:
    4. Letter();
    5. ~Letter();
    6. void setText(const QString &text);
    7. QString getText();
    8. protected:
    9. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    10. QRectF boundingRect() const;
    11. QPainterPath shape() const;
    12.  
    13. private:
    14. QString myText;
    15. QTextOption myTextOption;
    16. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Letter::Letter()
    2. {
    3. myTextOption.setAlignment(Qt::AlignCenter);
    4. myTextOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
    5. }
    6.  
    7. Letter::~Letter() {
    8.  
    9. }
    10.  
    11. QRectF Letter::boundingRect() const {
    12. return QRectF(0,0,145,145);
    13. }
    14.  
    15. QPainterPath Letter::shape() const {
    16. path.addRect(boundingRect());
    17. return path;
    18. }
    19.  
    20. void Letter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
    21. painter->fillRect(boundingRect(),Qt::red);
    22. painter->setPen(Qt::white);
    23. painter->setFont(QFont("Open Sans", 60));
    24. painter->drawText(boundingRect(),myText,myTextOption);
    25. QGraphicsTextItem::paint(painter,option,widget);
    26. }
    27.  
    28. void Letter::setText(const QString &text) {
    29. myText = text;
    30. this->update();
    31. }
    32.  
    33. QString Letter::getText() {
    34. return myText;
    35. }
    To copy to clipboard, switch view to plain text mode 
    I add letters to QGraphicsScene
    Qt Code:
    1. class GameField : public QGraphicsScene
    2. {
    3. public:
    4. GameField();
    5. };
    6.  
    7. GameField::GameField()
    8. {
    9. this->setBackgroundBrush(QBrush(QColor(230,230,230),Qt::SolidPattern));
    10. for (int i = 10; i < 610; i += 150) {
    11. for (int j = 10; j < 610; j += 150) {
    12. Letter *letter = new Letter();
    13. letter->setText(QString('A'+qrand()%26));
    14. this->addItem(letter);
    15. letter->setPos(i, j);
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    but when I try to click on any letter, my program crashes.
    Last edited by johnkrusty; 20th October 2012 at 10:15.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QGraphicsTextItem

    Why not use the QGraphicsTextItem getter/setter methods setPlainText() and toPlainText()?

    Then in your painter do this:
    Qt Code:
    1. void Letter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
    2. . . .
    3. painter->drawText(boundingRect(),toPlainText(),myTextOption);
    4. //QGraphicsTextItem::paint(painter,option,widget);
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to norobro for this useful post:

    johnkrusty (21st October 2012)

  4. #3
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QGraphicsTextItem

    Thank you for your response, it solved my problem with a crash. But now I have 2 letters inside the rectangle: first in the center (with QTextOption and Qpen white), second black (standard).
    Last edited by johnkrusty; 21st October 2012 at 10:24.

  5. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QGraphicsTextItem

    Removing the call to the parent class paint function (QGraphicsTextItem::paint()) should get rid of the black letter(s).

    Qt Code:
    1. void Letter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
    2. painter->fillRect(boundingRect(),Qt::red);
    3. painter->setPen(Qt::white);
    4. painter->setFont(QFont("Open Sans", 60));
    5. painter->drawText(boundingRect(),toPlainText(),myTextOption);
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to norobro for this useful post:

    johnkrusty (21st October 2012)

  7. #5
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QGraphicsTextItem

    Thanks, it's working perfectly now.

Similar Threads

  1. problem with QGraphicsTextItem's black dotted line.
    By jonike in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2011, 19:27
  2. Replies: 51
    Last Post: 26th November 2010, 14:24
  3. Customizing QGraphicsTextItem keyPressEvent problem
    By Lykurg in forum Qt Programming
    Replies: 6
    Last Post: 28th March 2009, 18:05
  4. Problem on set QGraphicsTextItem write protect.
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2007, 21:53
  5. QGraphicsTextItem focus problem
    By Gopala Krishna in forum Qt Programming
    Replies: 5
    Last Post: 26th June 2007, 18:25

Tags for this Thread

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.