Results 1 to 2 of 2

Thread: howto; create an image, fill pixels with color, display on scene

  1. #1
    Join Date
    Aug 2012
    Location
    The Netherland
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question howto; create an image, fill pixels with color, display on scene

    Hi there,

    I would like to create a image (on the fly), fill it with colors and then display it using a QGraphicsScene. I managed to write the code but the pixels that I made blue are not visible. Any ideas on what is wrong?

    * part of the view class (the main code regarding the question is in the initializeScene function):

    Qt Code:
    1. cGeometryViewer::cGeometryViewer(QWidget *parent) :
    2. QGraphicsView(parent)
    3. {
    4. m_scene = new QGraphicsScene(0, 0, MATRIXWIDTH, MATRIXHEIGHT);
    5. setScene(m_scene);
    6. }
    7.  
    8. void cGeometryViewer::setGeometry(cGeometry *geometry)
    9. {
    10. m_geometry = geometry;
    11. initializeScene();
    12. }
    13.  
    14. void cGeometryViewer::initializeScene()
    15. {
    16. QImage img(MATRIXWIDTH, MATRIXHEIGHT,QImage::Format_ARGB32);
    17.  
    18. //just to find out if it works fill a rectangle with black pixels
    19. for(int x=0; x<100; x++)
    20. for(int y=0; y<100; y++)
    21. img.setPixel(QPoint(x,y), Qt::black);
    22.  
    23. QGraphicsPixmapItem pmi(QPixmap::fromImage(img));
    24. m_scene->addItem(&pmi);
    25. show();
    26. }
    To copy to clipboard, switch view to plain text mode 

    * and part of the main code;

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. m_geometry = new cGeometry();
    8. m_viewer = new cGeometryViewer(ui->frameMain);
    9. m_viewer->setGeometry(m_geometry);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance!
    Rob

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: howto; create an image, fill pixels with color, display on scene

    You should create all QGraphicsPixmapItem on the heap, because they are going to be destroyed after outing of the function scope.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    robsofmar (26th August 2012)

Similar Threads

  1. Replies: 5
    Last Post: 17th December 2014, 12:07
  2. Replies: 1
    Last Post: 9th May 2011, 15:19
  3. Replies: 5
    Last Post: 23rd January 2011, 11:27
  4. How to fill QLabel Circle with color
    By arunvv in forum Qt Programming
    Replies: 6
    Last Post: 8th January 2009, 08:46
  5. How to fill the grid with the color?
    By merry in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2007, 11:10

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.