As Christian Ehrlicher said according to the Documentation QByteArray::fromRawData() only store the pointer to the data. That explain why it doensn't work outside the function since the data goes out of scope. Instead of using this method I used one the constructors that make deep copy of the data. And it just works fine!
QByteArray data (value.dataPointer (), value.dataSize ());
I have find a way to get a QImage of an QWindow:
Make a standard QWindow with is surface and add the following code:
// set the current context OpenGL
// check if an OpenGlContext is created
qFatal("Cannot create the requested OpenGL context!");
// init a new framebuffer
const QRect drawRect(0, 0, this->width(), this->height());
const QSize drawRectSize = drawRect.size();
_fbo = new QOpenGLFramebufferObject(drawRectSize, fboFormat);
_fboImage = new QImage(_fbo->toImage(false));
But, this is work only if the window is "visible", when it is set to Hidden the rendering of Qt3D was made (the rendering loop, because the GPU is used and the FPS is divided by 3), but the QOpenGLFramebufferObject was not filled :/
Do you have any idea of how force the filling of the fbo ?
The QPixmap class has a constructor that takes a QSize parameter and therefore allows you to create a pixmap of a given size. That is what @jsulm is doing in the first line of his last code example. Documentation.
QPainter can draw a QImage. So you can for example create a new Widget, subclass for QLabel for example if you want and reimplement paintEvent. Like this you work all the time with your QImage without creating another one.
And better you can only update the region that changed, no need to refresh everything. I did not try it but it should work.
Hi! The way to convert your matrix to a QPixmap is to convert it to QImage first (e.g. using void QImage::setPixel(const QPoint &position, uint index_or_rgb)) and then convert the QImage to a QPixmap (using [static] QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor)).
@Rohith : thanks.but your solution does not work for me. I have multi button. I for example want to set image (/pic/1.png) to btn1 and then set another image to another button.
My question How to set image to them in my code behind?
Thanks for the reply. This is using intel graphics, and unfortunately your workaround does not help. After your message, I've tested the same code from a newer live image and also on my more up-to-date laptop (which has similar hardware) and I can not reproduce the issue in either of these cases. On the affected machine, I tried updating the intel driver and mesa, but this did not solve it, so I guess it probably needs the kernel or qt packages updated as well. Unfortunately, this is not an option at this point.
I've worked around the issue by re-implementing paintEvent() to do something like the following
Which seems to work just fine. In fact, re-implementing the paintEvent has given me the chance to animate the image changes which looks pretty nice, and it will be easy to simplify the code a bit when all my machines are fully updated and the glitch is gone.
It's bananas to me that Qt doesn't have a composition mode that multiplies the alpha channels together.
Of course it does. At least couple of modes do that. But they do it in a more sensible way.
Multiplying values channel wise is a very weird operation when you talk about colors and I haven't seen it in any graphics API. You could argue that multiplying alphas has some sense in some situations, but color channels? To put it simply: what does "red times red" mean? That's the bananas. You might be getting something that you like by accident but it has little sense computationally. In most common composition modes you mix color channels with alpha values in one form or another.
eg. if you want to make the original image more transparent)
If you want to apply source alpha to the destination you would multiply the source alpha channel with the destination color channels, not its alpha channel alone. That's what for example QPainter::CompositionMode_DestinationIn mode does.
I agree. Since it will really depends on user machines.
I use ramdisk for saving so it is very fast, where old
hd will take a long time.
Besides I user makes full HD animation with 1000 frames,
I assume he do know it is a bit heavy :)
Just don't create it on heap. QPixmap is implicitly shared.
It does not matter how you create pixmap. What does is total number of allocated resources.
Changes to your code might fix something if your previous code did not clean the memory -did not delete pixmap.
So code causing problem was not posted.
Dzięki za szybką odpowiedź.
Problem rozwiązany zamiast QPixmap, użyłem QImage i metody drawImage().
Nadal nie mam pojęcia dlaczego QPixmap nie działa. QStringList zawiera ścieżki, a obrazy są formatu .jpg
This seems a misunderstanding of language basics.
Global and extern meaning are different.
I would highly advice reading the book.
In such context extern means that you want to access variable _corner which is declared and instantiated in other module.
Since it is not true you get a link error.
At the same time I would highly advice against global variables in C++.
Using such leads to code which is highly difficult to maintain.
If you really need something accessible globally you may either subclass QApplication or use singleton pattern
or at least use static class members .
No and that's the point, I want to resize my image with the optimized size to display it.
The size could be the size maximum that I can load depending on the memory available.
And so my question is precisely, how to calculate this optimized size for resizing my image depending on the memory available, to display my image even if it is too large (in a QPixmap added within a QGraphicsScene through a QGraphicsPixmapItem)?