PDA

View Full Version : QT4.3, addPix, QImage, and



gpratt
14th June 2007, 00:42
I'm an MFC guy trying out QT and trying to keep an open mind. Can anyone tell me what I'm doing wrong with the followin?

1) QGraphicsScene::addPixmap (or maybe QPixmap::fromImage) does not seem to work. When I display a pixmap using "setBackgroundBrush" it seems to work correctly, but when displaying it with addPixmap, it displays totally distorted. See "Code 1", below

2) There seems to be a problem creating an image with 8-bit indexed pixels. In fact, the sample code in the documentation does not work. Running the "Code 2", below results in error messages indicating that indexes 0, 1 and 2 are invalid. Stepping into the QT code seems to indicate that NumColors is not being properly set by the constructor (adding setNumColors(256) corrects the problem).

3) Why does Valgrind return so many problems with QT? (QT4 is MUCH better than qt3, but still has plenty of issues).

I first tried QT3, but quickly became discouraged by the seeminly random behavior (appears to stack corruption or maybe loose pointers). QT4 seems a little better in that respect, but seems to have other bugs. Any advice would be appreciated.

Thanks,

Gary

CODE1

#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QPointF>
#include <QPixmap>


int main( int argc, char ** argv ) {
//make the main application
QApplication myapp(argc, argv);
// make a main window
QGraphicsScene myscene;
myscene.setSceneRect(minX, minY, maxX, maxY);
myscene.setItemIndexMethod(QGraphicsScene::NoIndex );

// make a view on the canvas and enlarge it a bit
QGraphicsView myview(&myscene);
myview.resize(width*8, height*8);
myview.show();

QImage myimage(width+1, height+1, QImage::Format_RGB32);
for (int x=0; x<=width; x++) {
for (int y=0; y<=height; y++) {
myimage.setPixel(x,y,qRgb((x*y)%256,(x*y)%256,(x*y )%256));
}
}

//this displays the image properly
myview.setBackgroundBrush(myimage);

//this displays a totally distorted image
QPixmap mypixmap(width+1, height+1); mypixmap.fromImage(myimage);
QGraphicsPixmapItem * mypixmapitem = myscene.addPixmap(mypixmap);

int result = myapp.exec();
return result;
}
CODE2

#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QPointF>
#include <QPixmap>


int main( int argc, char ** argv ) {
//make the main application
QApplication myapp(argc, argv);
//note: this code described in file:///usr/local/Trolltech/Qt-4.3.0/doc/html/qimage.html#pixel-manipulation
//does not work. numColors() is not properly initialed in the constructor.
QImage image(3, 3, QImage::Format_Indexed8);
QRgb value;
value = qRgb(122, 163, 39); // 0xff7aa327
image.setColor(0, value);
value = qRgb(237, 187, 51); // 0xffedba31
image.setColor(1, value);
value = qRgb(189, 149, 39); // 0xffbd9527
image.setColor(2, value);
image.setPixel(0, 1, 0);
image.setPixel(1, 0, 0);
image.setPixel(1, 1, 2);
image.setPixel(2, 1, 1);
}

wysota
14th June 2007, 09:39
1) QGraphicsScene::addPixmap (or maybe QPixmap::fromImage) does not seem to work. When I display a pixmap using "setBackgroundBrush" it seems to work correctly, but when displaying it with addPixmap, it displays totally distorted. See "Code 1", below
QPixmap::fromImage() is a static method returning the pixmap created from the image, so your code should be:

QPixmap mypixmap=QPixmap::fromImage(myimage);


2) There seems to be a problem creating an image with 8-bit indexed pixels. In fact, the sample code in the documentation does not work. Running the "Code 2", below results in error messages indicating that indexes 0, 1 and 2 are invalid. Stepping into the QT code seems to indicate that NumColors is not being properly set by the constructor (adding setNumColors(256) corrects the problem).
If you use indexed images, you have to first set the colour lookup table size using QImage::setNumColors().


3) Why does Valgrind return so many problems with QT? (QT4 is MUCH better than qt3, but still has plenty of issues).
If you look closely, you'll notice that the problems are not related to Qt itself but to 3rd party libraries it uses (X11, libpng, etc.). AFAIK there are no memory problems with Qt.