PDA

View Full Version : problem with Qgraphicspixmapitem



skab
20th October 2010, 11:16
Hi all,

I need a QGraphicsPixmapItem that show pixel's value when you zoom. So I create my QGraphicsPixmapItem and reimplement paint method :


MyPixmapGraphicsItem::MyPixmapGraphicsItem(QPixmap p , QGraphicsItem * parent)
: pix(p)
, QGraphicsItem(parent)
{
image = pix.toImage();
}


void MyPixmapGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());

painter->drawPixmap (0,0, pix);

if (lod >= 30)
{
QPen pen;
int wStart, wLength;
int hStart, hLength;
int x1,y1;
QRgb pixval;
QString tr,tg,tb;
QRectF boundbox = option->exposedRect;

wStart = (int) (boundbox.x());
hStart = (int) (boundbox.y());
wLength = (int) (boundbox.width());
hLength = (int) (boundbox.height());

for(int i=hStart ; i<hStart+hLength ; i++)
{
for(int j=wStart ; j<wStart+wLength ; j++)
{
pixval=20;image.pixel(j,i);
tr.setNum(qRed(pixval));
tg.setNum(qGreen(pixval));
tb.setNum(qBlue(pixval));
if(pix.depth()==8)
{
if(qRed(pixval)<140)
pen.setColor( QColor::fromRgb(255,255,255));
else
pen.setColor( QColor::fromRgb(0,0,0) );

painter->setPen( pen );
painter->drawText(j,(int) i,tr);
}
else
{
QColor color = QColor::fromRgb(pixval);
if(color.value()<140)
pen.setColor( QColor::fromRgb(255,255,255));
else
pen.setColor( QColor::fromRgb(0,0,0));
painter->setPen( pen );
tr.insert(0,"R:");tg.insert(0,"G:");tb.insert(0,"B:");
painter->drawText(j,i+2,tr);
painter->drawText(j,i+3,tg);
painter->drawText(j,i+4,tb);
}
}
}
}
}


"Pix" variable is my pixmap and "image" is a Qimage (pix copy) created by MyPixmapGraphicsItem constructor.

This code not work because I draw pixel value on the entire image. How can I draw pixel value on the display window only.

Thanks in advance

tbscope
21st October 2010, 05:29
This code not work because I draw pixel value on the entire image. How can I draw pixel value on the display window only.

Can you explain the difference between "on the entire image" and "on the display window only"?

skab
21st October 2010, 10:21
"display windows only" : When you zoom, scrollbar appears and you only see a small part of the entire image. I want to refresh pixel value only on that.

Currently, I think that is too long to refresh all the image (it freezes my application).

genjix
23rd October 2010, 09:54
here's how I would do it:
subclass QGraphicsView and override paint(). Draw your number in that function and then call QGraphicsView::paint() to draw the zoomed scene.