Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, May 2007
[Newbie] Painting pixels on a QPixmap


Message 1 in thread

Hello... I am trying to figure how to change the color of few pixels on a 
QPixel image..  (I do not even know if this is the right approach.. 
suggestions are welcome)

I have to display some maps from my main program.. this maps are firstly 
generated from data vectors and then I'd like to change individual pixels on 
a frequent base. I'm not interested now in zooms*, pan and so on.. and I' 
searching for the simplest solution, as I am very confused with GUI 
programming... this is what I did so far:

QLabel -> QPixmal
..and now I don't know how to change individual QPixmal pixels :-(((

Alternativelly there is the approach of chapter 5 of the "C++ GUI programming 
with Qt4" book, where it is created a custom widget with a QImage and then 
painted over it with a QRect.. just seems too complex or it is the right 
approach ??

*however it may happen that my map is bigger than the allocated space on the 
widget, so a rescaling is required, this is why I set the QLabel propriety 
scaledContents to true..

Sylvaticus



--
 [ signature omitted ] 

Message 2 in thread

Hello Sylvaticus,

one easy way to paint something on a pixmap is using QPainter:

QPixmap myPixmap;
QPainter imagePainter( &myPixmap ); // Initilialize the painter with the 
image

// Paint some things into the pixmap
painter.drawPoint( 1,2 );
...

When using QPainter you can define the brush and the style of your 
lines, etc. Also you can define a pen (with some color, etc.).

I hope that helps.

Best Regards,
Falko

Antonello Lobianco schrieb:
> Hello... I am trying to figure how to change the color of few pixels on a 
> QPixel image..  (I do not even know if this is the right approach.. 
> suggestions are welcome)
>
> I have to display some maps from my main program.. this maps are firstly 
> generated from data vectors and then I'd like to change individual pixels on 
> a frequent base. I'm not interested now in zooms*, pan and so on.. and I' 
> searching for the simplest solution, as I am very confused with GUI 
> programming... this is what I did so far:
>
> QLabel -> QPixmal
> ..and now I don't know how to change individual QPixmal pixels :-(((
>
> Alternativelly there is the approach of chapter 5 of the "C++ GUI programming 
> with Qt4" book, where it is created a custom widget with a QImage and then 
> painted over it with a QRect.. just seems too complex or it is the right 
> approach ??
>
> *however it may happen that my map is bigger than the allocated space on the 
> widget, so a rescaling is required, this is why I set the QLabel propriety 
> scaledContents to true..
>
> Sylvaticus
>   

--
 [ signature omitted ] 

Message 3 in thread

thanks for quickest ever answer ;-) ..gonna try..

On Monday 14 May 2007 12:34, Falko Buttler wrote:
> Hello Sylvaticus,
>
> one easy way to paint something on a pixmap is using QPainter:
>
> QPixmap myPixmap;
> QPainter imagePainter( &myPixmap ); // Initilialize the painter with the
> image
>
> // Paint some things into the pixmap
> painter.drawPoint( 1,2 );
> ...
>
> When using QPainter you can define the brush and the style of your
> lines, etc. Also you can define a pen (with some color, etc.).
>
> I hope that helps.
>
> Best Regards,
> Falko
>
> Antonello Lobianco schrieb:
> > Hello... I am trying to figure how to change the color of few pixels on a
> > QPixel image..  (I do not even know if this is the right approach..
> > suggestions are welcome)
> >
> > I have to display some maps from my main program.. this maps are firstly
> > generated from data vectors and then I'd like to change individual pixels
> > on a frequent base. I'm not interested now in zooms*, pan and so on.. and
> > I' searching for the simplest solution, as I am very confused with GUI
> > programming... this is what I did so far:
> >
> > QLabel -> QPixmal
> > ..and now I don't know how to change individual QPixmal pixels :-(((
> >
> > Alternativelly there is the approach of chapter 5 of the "C++ GUI
> > programming with Qt4" book, where it is created a custom widget with a
> > QImage and then painted over it with a QRect.. just seems too complex or
> > it is the right approach ??
> >
> > *however it may happen that my map is bigger than the allocated space on
> > the widget, so a rescaling is required, this is why I set the QLabel
> > propriety scaledContents to true..
> >
> > Sylvaticus
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

-- 
 [ signature omitted ] 

Message 4 in thread

Sylvaticus,

Depending on how much pixel manipulation you want to do, you should consider
converting the QPixmap to a QImage. From the Qt 4 docs:

Qt provides four classes for handling image data: QImage, QPixmap, QBitmap
and QPicture. QImage is designed and optimized for I/O, and for direct pixel
access and manipulation, while QPixmap is designed and optimized for showing
images on screen.

QImage has methods like pixel() to read an individual pixel, and setPixel()
to set its value. You can convert a QPixmap to a QImage using
QPixmap::toImage(), and the other way using QPixmap::fromImage().

Happy coding,
Tom

On 5/14/07, Antonello Lobianco <blackhole@xxxxxxxxxxxx> wrote:
>
> Hello... I am trying to figure how to change the color of few pixels on a
> QPixel image..  (I do not even know if this is the right approach..
> suggestions are welcome)
>
> I have to display some maps from my main program.. this maps are firstly
> generated from data vectors and then I'd like to change individual pixels
> on
> a frequent base. I'm not interested now in zooms*, pan and so on.. and I'
> searching for the simplest solution, as I am very confused with GUI
> programming... this is what I did so far:
>
> QLabel -> QPixmal
> ..and now I don't know how to change individual QPixmal pixels :-(((
>
> Alternativelly there is the approach of chapter 5 of the "C++ GUI
> programming
> with Qt4" book, where it is created a custom widget with a QImage and then
> painted over it with a QRect.. just seems too complex or it is the right
> approach ??
>
> *however it may happen that my map is bigger than the allocated space on
> the
> widget, so a rescaling is required, this is why I set the QLabel propriety
> scaledContents to true..
>
> Sylvaticus
>
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

Message 5 in thread

Thanks, the problem is that then I don't know then how to display the 
QImage...

Using QPixel I wrote:

myQPixmap = QPixmap(QString::fromUtf8(":/imgs/mapStart.png"));
//myQImage Â= ÂQImage(QString::fromUtf8(":/imgs/mapStart.png"));
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ
mapAreaLabel->setPixmap(myQPixmap);
QPainter painter( &myQPixmap ); 
painter.setPen(Qt::blue);
 for (int i=100;i<200;i++){  Â// jus as I can't see easily one pixel ;-)
  for (int j=100;j<200;j++){
   painter.drawPoint( i,j );
  }ÂÂÂ
 }

Hovewer the mapArea is not upgraded when draw the point.. I think because 
setPixmap() doesn't take a pointer... so, to show the plue painted pixels I 
have to give again 
mapAreaLabel->setPixmap(myQPixmap);
That is very resource intensive...

My problem is that I have to change few pixels and see them imediatly, 
hopefully without copy or redraw the whole map.

Shgould I use instead a frame and associate (how?) a QImage to it to then use 
the setPixel() method??

(PS Qt Designer doesn't have a native QImage widget to place it on the 
form...)



On Monday 14 May 2007 13:07, Tom Panning wrote:
> Sylvaticus,
>
> Depending on how much pixel manipulation you want to do, you should
> consider converting the QPixmap to a QImage. From the Qt 4 docs:
>
> Qt provides four classes for handling image data: QImage, QPixmap, QBitmap
> and QPicture. QImage is designed and optimized for I/O, and for direct
> pixel access and manipulation, while QPixmap is designed and optimized for
> showing images on screen.
>
> QImage has methods like pixel() to read an individual pixel, and setPixel()
> to set its value. You can convert a QPixmap to a QImage using
> QPixmap::toImage(), and the other way using QPixmap::fromImage().
>
> Happy coding,
> Tom
>
> On 5/14/07, Antonello Lobianco <blackhole@xxxxxxxxxxxx> wrote:
> > Hello... I am trying to figure how to change the color of few pixels on a
> > QPixel image..  (I do not even know if this is the right approach..
> > suggestions are welcome)
> >
> > I have to display some maps from my main program.. this maps are firstly
> > generated from data vectors and then I'd like to change individual pixels
> > on
> > a frequent base. I'm not interested now in zooms*, pan and so on.. and I'
> > searching for the simplest solution, as I am very confused with GUI
> > programming... this is what I did so far:
> >
> > QLabel -> QPixmal
> > ..and now I don't know how to change individual QPixmal pixels :-(((
> >
> > Alternativelly there is the approach of chapter 5 of the "C++ GUI
> > programming
> > with Qt4" book, where it is created a custom widget with a QImage and
> > then painted over it with a QRect.. just seems too complex or it is the
> > right approach ??
> >
> > *however it may happen that my map is bigger than the allocated space on
> > the
> > widget, so a rescaling is required, this is why I set the QLabel
> > propriety scaledContents to true..
> >
> > Sylvaticus
> >
> >
> >
> > --
> > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> > "unsubscribe" in the subject or the body.
> > List archive and information: http://lists.trolltech.com/qt-interest/

-- 
 [ signature omitted ] 

Message 6 in thread

On Monday 14 May 2007 14:11, Antonello Lobianco wrote:
> Thanks, the problem is that then I don't know then how to display the
> QImage...

I managed creating my own class "MapBox" and then adding a widget in 
QTDesigner and promoting it to "MapBox".
MapBox has its own QImage object ("layer").


> > Hello... I am trying to figure how to change the color of few pixels on a
> > QPixel image..  (I do not even know if this is the right approach..
> > suggestions are welcome)
> >
> > I have to display some maps from my main program.. this maps are firstly
> > generated from data vectors and then I'd like to change individual pixels
> > on a frequent base. I'm not interested now in zooms*, pan and so on.. and
> > I' searching for the simplest solution, as I am very confused with GUI
> > programming..


This is the code of the paintEvent(...) reimplementation and of 
updatePixel(...) function to update only one pixel at a time:

----------------------------------------------------------------------------------------
void MapBox::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	QRect rect;
	QColor color;
	for (int i = 0; i < layer.width(); ++i) {
		for (int j = 0; j < layer.height(); ++j) {
			rect = QRect( i,  j, 1, 1);
			if (!event->region().intersect(rect).isEmpty()) {
				color = QColor::fromRgba(layer.pixel(i, j));
				painter.setPen(color);
				painter.drawPoint( i,j );
			}
		}
	}
}
--------------------------------------------------------------------------------------------
void MapBox::updatePixel(int x_h, int y_h, QColor color_h){
	layer.setPixel(x_h, y_h, color_h.rgba());
	//update(x_h,y_h,1,1);
	update();
}
----------------------------------------------------------------------------------------------

It works, but when I call for testings updatePixel() too frequently (e.g. in a 
loop on x,y ) I have the following error:

*** glibc detected *** corrupted double-linked list: 0xb75764f8 ***

Am I going wrong somewhere???

/Sylvaticus

--
 [ signature omitted ]