Results 1 to 5 of 5

Thread: Problems with QGraphicsItem setPos

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with QGraphicsItem setPos

    What is your first code snippet supposed to do?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2011
    Posts
    30
    Thanks
    1

    Default Re: Problems with QGraphicsItem setPos

    The first code snippet draws a collection of pixmaps in a grid like:
    a a a a
    a a a a

    The second code snippet draws text formed by a set of pixmaps (letters).

    It will be so good if you can tell me what is wrong with the coordinate system. I start draw at 0,0. At the second code snippet everything is ok. The way I set coordinates to draw pixmaps is the same in both code snippets.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with QGraphicsItem setPos

    The code looks fine, maybe the problem is elsewhere?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2011
    Posts
    30
    Thanks
    1

    Default Re: Problems with QGraphicsItem setPos

    Ok, I have found the problem. I had only one item at the scene, so the position was correct but it was translated. When I added QGraphicsPixmapItem at 0,0 my custom graphics item appeared at the right position.
    Last edited by zzz9; 9th October 2011 at 11:06. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.