06-20-2012 12:28 PM - edited 06-20-2012 12:49 PM
Much of this issue has been discussed here:
Basically, a suggested correct way of converting a QImage to ImageView is this one:
PixelBufferData pbd (PixelBufferData::RGBA_PRE,yourQImage.width(),your
QImage.height(),yourQImage.width(),yourQImage.bits ()); yourImageView->setImage(pbd);
Then there was someone specifying that the format should probably be BGR instead of RGB, which could be done with OpenGL or like this:
Here is my code I have a problem with:
PixelBufferData pbd(PixelBufferData::RGBA_PRE, orgPattern.width(), orgPattern.height(), orgPattern.width(), orgPattern.rgbSwapped().bits()); m_patternFrame->setImage(pbd);
orgPattern is a QImage loaded from a file path that is certain to exist. m_patternFrame is a pointer to ImageView.
I know that m_patternFrame is a valid pointer because if I call its method setImageSource with the path I created the QImage from, it displays that picture. But when I run that code, this is printed in the console:
Context: Failed to find target node with id %d 906.
Some times the number is different. When I tested again, it was 563. The application doesn't cursh or freeze, but nothing happens with the frame, it's still blank.
Can anyone provide any help with this issue?
Solved! Go to Solution.
06-20-2012 03:14 PM
Thank you for starting a new thread and referencing another thread. That helps us give you a timely response.
Before we delve into this too far, what is the width and height? (We are within bounds, I assume?)
And can you try doing the setImage in 2 steps: first create the Image from the pdb rather than implicitly, and test for !image.isNull()
06-20-2012 04:49 PM - edited 06-20-2012 06:30 PM
Thanks for the quick reply. You were right, the image initially was null and that was generating the context issue.
However, now I have a potentially deeper issue. After I fixed the image so it is correct (and verified this by exporting it and checking), I found out that now it freezes the application if I try to set the buffer to the frame, or create a bb::cascades::Image from it. Here is what I tried (and it made no change, still freezes):
- dynamic allocation of QImage
- RGBX instead of RGBA_PRE; the QImage was created with either QImage::Format_RGB32 for the first type, or QImage::Format_ARGB32_Premultiplied for the second type.
- removed rgbSwapped();
- changed width() with bytesPerLine() on the fourth argument
This is the creation code:
QImage currentPattern(600, 600, QImage::Format_RGB32); currentPattern.fill(QColor::fromRgb(127,127,127)); QPainter painter; painter.begin(¤tPattern); //... //Some calls to painter.drawImage removed for brevity //... painter.end(); PixelBufferData pbd(PixelBufferData::RGBX, currentPattern.width(), currentPattern.height(), currentPattern.width(), currentPattern.rgbSwapped().bits()); printf("Buffer created!\n"); fflush(stdout); bb::cascades::Image img(pbd); printf("Image created!\n"); fflush(stdout); m_patternFrame->setImage(img); printf("Function has finished!\n"); fflush(stdout);
Basically, it never gets to print out: "Image created!"
m_patternFrame is a pointer to ImageView which has a blank QUrl set as a source, i.e. it is empty so far. There is a workaround around this whole buffer thing, (which is exporting the image and reading it through setImageSource), but that workaround is performance kill if I need to use this frequently.
06-21-2012 10:31 AM - edited 06-21-2012 10:35 AM
I'm going to play with this, but in the meantime, please see:
Restrict yourself to Qt classes documented in:
06-21-2012 10:48 AM - edited 06-21-2012 10:53 AM
I was hoping this would lead to a workaround for what your are trying to do in R4.
Please take a look at:
If this affects you, please vote for it.
I don't see an issue directly on image manipulation. If you feel you need additional features, please request a feature for it in JIRA. The only workaround I see at the moment is to use routines to manipulate the underlying image data.
06-21-2012 11:19 AM - edited 06-21-2012 11:20 AM
Hey, thanks for the quick replies. It just comes up that I managed to "work-around" this issue somehow. I saw in the topic you suggested that QPainter isn't fully supported, though it seems to work fine for me.
I'll post the actual solution now. Thing is, I had to copy image buffer data on memory I allocated dynamically. Then I used this buffer to create PixelBufferData. Even though it is slow, it is probably an effective workaround until the image creation is fixed and can be done safely from QImage::bits(). Here is the code:
QImage swapped = m_displayCopy.rgbSwapped(); const uchar* pointer = swapped.constBits(); int pixels_total = swapped.width() * swapped.height() * 4; unsigned int* buffer = (unsigned int*)malloc(pixels_total); memset(buffer, 0xff, pixels_total); //Set all to white first memcpy(buffer, pointer, qMin(pixels_total, swapped.byteCount())); //The qMin is some sort of a safety check
PixelBufferData pbd(PixelBufferData::RGBX, swapped.width(), swapped.height(), swapped.width(), buffer); m_image->setImage(pbd); free(buffer);
And this works. I'll mark your first answer as a solution as it is truly the solution to the first problem.