PDA

View Full Version : Problems with QGraphicsItem setPos



zzz9
9th October 2011, 09:05
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.

QRectF GraphicsList :: boundingRect() const
{
return QRectF(0, 0, width, height);
}

void GraphicsList :: paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
if (fnt == 0) return;

painter->setClipping(true);
painter->setClipRect(0, 0, boundingRect().width(), boundingRect().height());

int xpos = 0, ypos = 0, maxheight = minitemheight;
for (GraphicsListItem::List::iterator i = Items.begin(); i != Items.end(); ++i)
{
GraphicsListItem* item = *i;

painter->drawPixmap(xpos, ypos - offset, item->pixmap);

if (item->pixmap.height() > minitemheight)
if (item->pixmap.height() > maxheight)
maxheight = item->pixmap.height();

xpos += itemwidth + 20;

if (xpos > boundingRect().width())
{
ypos += maxheight + 20;
xpos = 0;
}
}
}

Class that works well.

QRectF GraphicsScrollText :: boundingRect() const
{
return QRectF(0, 0, width, height);
}

void GraphicsScrollText :: paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
if (fnt == 0) return;

painter->setClipping(true);
painter->setClipRect(0, 0, boundingRect().width(), boundingRect().height());

int line = 0, xpos = 0;
QStringList list = text.split(' ', QString::SkipEmptyParts);
for (QStringList::iterator i = list.begin(); i != list.end(); ++i)
{
int w = fnt->textWidth(*i);
if (w + xpos > boundingRect().width())
{
++line;
xpos = 0;
}

//if (line * 20 > boundingRect().height()) return;

for (int j = 0; j < (*i).length(); ++j)
{
char t = (*i).at(j).toAscii();

if (fnt->letters.contains(t))
{
painter->drawPixmap(xpos, -offset + line * 20, fnt->letters[t]->pixmap);
xpos += fnt->letters[t]->pixmap.width();
}
else
xpos += 10;
}

xpos += 10;
}
}

wysota
9th October 2011, 09:43
What is your first code snippet supposed to do?

zzz9
9th October 2011, 10:21
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.

wysota
9th October 2011, 10:53
The code looks fine, maybe the problem is elsewhere?

zzz9
9th October 2011, 11:06
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.