Results 1 to 5 of 5

Thread: QGraphicsView (using int array) advice and display help

  1. #1
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsView (using int array) advice and display help

    I receive image data from an API as an int pointer to an array laid out in memory like a C array x[i][j][k]

    What would be the recommended way to display this image in a graphics view?
    I am currently using a nested loop to loop through the entire array and "QImage::setPixel(x,y,Qrgb)" to set every the color of every pixel. Then I turn the QImage into a Qpixmap, add it to a GraphicsScene, then link it to a GraphicsView.
    Last edited by enricong; 10th April 2012 at 03:45.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGraphicsView (using int array) advice and display help

    What would be the recommended way to display this image in a graphics view?
    We have no idea what your three-dimensional integer array data represents or what a logical way to display it would be. How does a 3d array represent image data? Is it a stack of multiple images?
    I am currently using a nested loop to loop through the entire array and "QImage::setPixel(x,y,Qrgb)" to set every the color of every pixel.
    Where do x, y, and the arguments for the colour come from in the three-dimensional array?

  3. #3
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView (using int array) advice and display help

    Sorry, I should have provided more info

    it is an int array of width,height,color
    color is a RGB triplet of three integers (0-255)

    Qt Code:
    1. for (int i=0; i<height; i++)
    2. for (int j=0; j<width; j++)
    3. {
    4. int pix = i*width*3+j*3;
    5. img->setPixel(i,j,qRgb(data[pix],data[pix+1],data[pix+2]));
    6. }
    To copy to clipboard, switch view to plain text mode 

    The above codes works but I don't know if there is a better way to do this
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGraphicsView (using int array) advice and display help

    If the data is an array of ints (i.e. 4 byte numbers) then there's no better way I am aware of.

    If the array is actually of bytes (unsigned char) then:
    Qt Code:
    1. // Some test data: grey gradient 64x64 pixel image
    2. uchar data[64 * 64 * 3];
    3. int offset = 0;
    4. for (int i = 0; i < 64; ++i) {
    5. for (int j = 0; j < 64; ++j) {
    6. data[offset++] = (i+j)*2;
    7. data[offset++] = (i+j)*2;
    8. data[offset++] = (i+j)*2;
    9. }
    10. }
    11.  
    12. // Create an image from that
    13. QImage image(data, 64, 64, 64 * 3, QImage::Format_RGB888);
    14. image.save("test.png");
    To copy to clipboard, switch view to plain text mode 
    works.

  5. #5
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView (using int array) advice and display help

    I did not realize that is what RGB888 meant. I am able to request the size of the int so I was able to put it in a uchar.

    Thanks
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. Display an image in a QGraphicsview
    By emilio in forum Newbie
    Replies: 8
    Last Post: 6th December 2011, 06:55
  3. General advice about scaling on QGraphicsView/QGraphicsScene
    By onurozcelik in forum Qt Programming
    Replies: 0
    Last Post: 24th May 2010, 08:14
  4. Display an image from a unsigned char array
    By Percy in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2009, 20:16
  5. Display Images in QGraphicsView
    By robertaandrews in forum Qt Tools
    Replies: 4
    Last Post: 15th May 2007, 18:35

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.