PDA

View Full Version : Create pixmap image from string



Morea
17th November 2006, 14:19
Hi.
I have a string and would like to create a QPixmap from the string. Each letter should correspond to a specific colour.
Each letter in the string should be turned into a 20x20 rect filled with the colour.
So a chess board could be created from the string bwbwbwbwbwbwbw...
Now, how do I do this?

jacek
17th November 2006, 14:40
Just create a pixmap of the right size and use QPainter:

QPainter p( &pixmap );
QRect rect(...);
for each letter:
rect.moveTo( ... );
p.fillRect( rect, ... );
p.end();

^NyAw^
17th November 2006, 15:29
Hi,
Are you sure taht you can use QPixmap? You only can create the image with XPM format.

If you use QImage you would be able to acces directly to the image pointer stored in uchar* by using "bits()". If the image would be a RGB you have to define that the QImage will be a RGB image.

Then you could read the string. For every character you could take the letter and use the proper RGB value (use the macro QRbg(int,int,int)). For example:

You read "b"(black), so the color is QRgb(255,255,255).
You read "w"(white), so the color is QRgb(0,0,0).
I don't remeber if I inverted the values. Black = 0,0,0 and Withe = 255,255,255 ? Try it.

Now, you have wich color is the character that you readed, you have to insert it 20 times in the "bits()" pointer. It is a pointer to an array, so you could index it like "data[i]".

You have to create the image with a width*20 and height*20.

It probably be more easy to program if you index the image with setPixel that you are able to index the 20*20 rectangle putting the QRgb value.

jacek
17th November 2006, 15:59
Are you sure taht you can use QPixmap?
Yes, I'm sure.


You only can create the image with XPM format.
No, that's only one out of 6 QPixmap constructors. You can always create an empty pixmap of given size and paint on it using QPainter.

^NyAw^
17th November 2006, 16:12
Ups, sorry for confusing.

The QPainter solution is easyier.

spud
17th November 2006, 16:38
I would also suggest working with a QImage, which allows pixel-precise manipulation. You can always convert it to a QPixmap later.
i.e.


QImage image(20,20, QImage::Format_ARGB32);
for(int i=0;i<400;i++)
{
QRgb rgb = string[i]=='b' ? qRgb(0,0,0) : qRgb(255,255,255);
image.setPixel(i%20, i/20, rgb);
}
QPixmap pixmap = QPixmap::fromImage(image);