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
);
}
{
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;
}
}
}
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;
}
}
}
To copy to clipboard, switch view to plain text mode
Class that works well.
QRectF GraphicsScrollText
:: boundingRect() const {
return QRectF(0,
0, width, height
);
}
{
if (fnt == 0) return;
painter->setClipping(true);
painter->setClipRect(0, 0, boundingRect().width(), boundingRect().height());
int line = 0, xpos = 0;
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;
}
}
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;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks