PDA

View Full Version : setPixmap and Paint Event



Magu
10th March 2010, 12:32
Hi all,

I´m trying to do some simple Qt experiments... like load and manipulate pixmaps.

Why this piece of code does not work?



image = QImage(256, 256, QImage::Format_RGB32);
QRgb value;
value = qRgb(189, 149, 39); // 0xffbd9527
image.setPixel(1, 1, value);
value = qRgb(122, 163, 39); // 0xff7aa327
image.setPixel(0, 1, value);
image.setPixel(1, 0, value);
value = qRgb(237, 187, 51); // 0xffedba31
image.setPixel(2, 1, value);
pixmap.fromImage(image,Qt::ColorOnly);
ui.label->setPixmap(pixmap);


As you can see, is a part of one example from Qt Image documentation. This code is placed in a slot, and it is connected to a button (or not, it is not important).
I only want to load and show a pixmap when the button has been pressed, at least as a first approx.
Now i´m reading something related to paint Events, and maybe this is the way to manipulate pixmap, but i have no much time.

Thanks

high_flyer
10th March 2010, 12:48
Why this piece of code does not work?
What do you mean by "not work"?
What do you see?

Magu
10th March 2010, 15:03
Thanks for your answer,

When the button is pressed, it seems that nothings occurs. However, if i load a bmp file, or jpg.. the file is loaded and shown when i signal the slot.

I think that i didn´t understand anything about Qimage, and maybe i need to read more about Qpainter?

Sorry about my english, but I ´ll try to explain what is supposed I want to do:

I need to show in a user interface several data patterns. These patterns are 2-Dim arrays of 2 bytes words. Like a PAL video frame, more or less. First of all, i want to show these patterns and make some changes in them, pixel by pixel. (like a inversion operation).
Also, these 16bits words need to be converted to an apropiate color, something like first possible value (0x0000) will be a dark blue color, and the last (0xFFFF) will be dark red.

What is the best way to do this?

Thanks again

high_flyer
10th March 2010, 16:37
When the button is pressed, it seems that nothings occurs.
can you show the connect() line you are using to connect the button to the slot?
Also, when you start the program from a console or a debugger, look at the output, see if any signal/slot connection warnings come up.
Once we solved the signal/slot connection we can look in to the rest.

Magu
10th March 2010, 16:49
Its a simple (i thing) connect:




connect (ui.paintButton, SIGNAL (clicked()), this, SLOT (initPixmap()));

.

Interface has been created with Qt Designer, so "ui" represents that object.
initPixmap() function contains the code I posted before.

Same line works if i load a bmp file:




pixmap.load("xxxx.bmp",const char *,Qt::Flags);
ui.label->setPixmap (pixmap);



, and there aren´t console message indicating some like "No such slot/signal...."

I think i´m missing some important concept about Qt graphics framework.

thanks again

high_flyer
11th March 2010, 08:32
Same line works if i load a bmp file:
So if you put these two lines in the slot instead of the QImage conversion code, you get an image when the button is clicked?

I see no problems in the code you posted so far.


I think i´m missing some important concept about Qt graphics framework.
You are not doing any painting your self, so it plays no role, at least for the code you posted.

spud
11th March 2010, 10:16
The problem is the following line:


pixmap.fromImage(image,Qt::ColorOnly);
It should be:


pixmap = QPixmap::fromImage(image,Qt::ColorOnly);

high_flyer
11th March 2010, 10:32
I fall in that pit every time! :-)
Well spotted!