Results 1 to 5 of 5

Thread: Problem with QGraphicsTextItem

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 09:15.

Similar Threads

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