Results 1 to 5 of 5

Thread: Problems with QGraphicsItem setPos

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2011
    Posts
    30
    Thanks
    1

    Default Problems with QGraphicsItem setPos

    I have two classes derived from QGraphicsItem. The first class works well, and whenever I use setPos the item appears on the right position. But the problem with the second class, item is always drawn at 0,0, but x() and y() return correct values.

    I'm really confused with boundingRect(), pos(), setClipRect() functions. Can anyone help?

    Can anyone also suggest ready classes for QGraphicsView that implement standart list and another classes.

    Class that doesn't work.
    Qt Code:
    1. QRectF GraphicsList :: boundingRect() const
    2. {
    3. return QRectF(0, 0, width, height);
    4. }
    5.  
    6. void GraphicsList :: paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    7. QWidget *widget)
    8. {
    9. if (fnt == 0) return;
    10.  
    11. painter->setClipping(true);
    12. painter->setClipRect(0, 0, boundingRect().width(), boundingRect().height());
    13.  
    14. int xpos = 0, ypos = 0, maxheight = minitemheight;
    15. for (GraphicsListItem::List::iterator i = Items.begin(); i != Items.end(); ++i)
    16. {
    17. GraphicsListItem* item = *i;
    18.  
    19. painter->drawPixmap(xpos, ypos - offset, item->pixmap);
    20.  
    21. if (item->pixmap.height() > minitemheight)
    22. if (item->pixmap.height() > maxheight)
    23. maxheight = item->pixmap.height();
    24.  
    25. xpos += itemwidth + 20;
    26.  
    27. if (xpos > boundingRect().width())
    28. {
    29. ypos += maxheight + 20;
    30. xpos = 0;
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    Class that works well.
    Qt Code:
    1. QRectF GraphicsScrollText :: boundingRect() const
    2. {
    3. return QRectF(0, 0, width, height);
    4. }
    5.  
    6. void GraphicsScrollText :: paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    7. QWidget *widget)
    8. {
    9. if (fnt == 0) return;
    10.  
    11. painter->setClipping(true);
    12. painter->setClipRect(0, 0, boundingRect().width(), boundingRect().height());
    13.  
    14. int line = 0, xpos = 0;
    15. QStringList list = text.split(' ', QString::SkipEmptyParts);
    16. for (QStringList::iterator i = list.begin(); i != list.end(); ++i)
    17. {
    18. int w = fnt->textWidth(*i);
    19. if (w + xpos > boundingRect().width())
    20. {
    21. ++line;
    22. xpos = 0;
    23. }
    24.  
    25. //if (line * 20 > boundingRect().height()) return;
    26.  
    27. for (int j = 0; j < (*i).length(); ++j)
    28. {
    29. char t = (*i).at(j).toAscii();
    30.  
    31. if (fnt->letters.contains(t))
    32. {
    33. painter->drawPixmap(xpos, -offset + line * 20, fnt->letters[t]->pixmap);
    34. xpos += fnt->letters[t]->pixmap.width();
    35. }
    36. else
    37. xpos += 10;
    38. }
    39.  
    40. xpos += 10;
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by zzz9; 9th October 2011 at 09:16. Reason: updated contents

Similar Threads

  1. QGraphicsItem.setPos takes a lot of time
    By ricofe25 in forum Qt Programming
    Replies: 2
    Last Post: 8th December 2009, 19:28
  2. QGraphicsItem setPos
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2009, 12:29
  3. QGraphicsItem setpos()
    By qtuser20 in forum Qt Programming
    Replies: 6
    Last Post: 3rd August 2009, 17:17
  4. QGraphicsItem pos() and setpos()
    By jimc1200 in forum Qt Programming
    Replies: 6
    Last Post: 16th March 2009, 18:51
  5. Stuck with QGraphicsItem's setPos() method
    By salmanmanekia in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2008, 10:05

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
  •  
Qt is a trademark of The Qt Company.