Results 1 to 2 of 2

Thread: QT4.3, addPix, QImage, and

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2007
    Posts
    1
    Thanks
    1

    Default QT4.3, addPix, QImage, and

    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
    Qt Code:
    1. #include <QtGui>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsItem>
    4. #include <QPointF>
    5. #include <QPixmap>
    6.  
    7.  
    8. int main( int argc, char ** argv ) {
    9. //make the main application
    10. QApplication myapp(argc, argv);
    11. // make a main window
    12. QGraphicsScene myscene;
    13. myscene.setSceneRect(minX, minY, maxX, maxY);
    14. myscene.setItemIndexMethod(QGraphicsScene::NoIndex);
    15.  
    16. // make a view on the canvas and enlarge it a bit
    17. QGraphicsView myview(&myscene);
    18. myview.resize(width*8, height*8);
    19. myview.show();
    20.  
    21. QImage myimage(width+1, height+1, QImage::Format_RGB32);
    22. for (int x=0; x<=width; x++) {
    23. for (int y=0; y<=height; y++) {
    24. myimage.setPixel(x,y,qRgb((x*y)%256,(x*y)%256,(x*y)%256));
    25. }
    26. }
    27.  
    28. //this displays the image properly
    29. myview.setBackgroundBrush(myimage);
    30.  
    31. //this displays a totally distorted image
    32. QPixmap mypixmap(width+1, height+1); mypixmap.fromImage(myimage);
    33. QGraphicsPixmapItem * mypixmapitem = myscene.addPixmap(mypixmap);
    34.  
    35. int result = myapp.exec();
    36. return result;
    37. }
    To copy to clipboard, switch view to plain text mode 
    CODE2
    Qt Code:
    1. #include <QtGui>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsItem>
    4. #include <QPointF>
    5. #include <QPixmap>
    6.  
    7.  
    8. int main( int argc, char ** argv ) {
    9. //make the main application
    10. QApplication myapp(argc, argv);
    11. //note: this code described in file:///usr/local/Trolltech/Qt-4.3.0/doc/html/qimage.html#pixel-manipulation
    12. //does not work. numColors() is not properly initialed in the constructor.
    13. QImage image(3, 3, QImage::Format_Indexed8);
    14. QRgb value;
    15. value = qRgb(122, 163, 39); // 0xff7aa327
    16. image.setColor(0, value);
    17. value = qRgb(237, 187, 51); // 0xffedba31
    18. image.setColor(1, value);
    19. value = qRgb(189, 149, 39); // 0xffbd9527
    20. image.setColor(2, value);
    21. image.setPixel(0, 1, 0);
    22. image.setPixel(1, 0, 0);
    23. image.setPixel(1, 1, 2);
    24. image.setPixel(2, 1, 1);
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 14th June 2007 at 09:30. Reason: missing [code] tags

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.