Results 1 to 4 of 4

Thread: QImage & Pixel manipulation

  1. #1
    Join Date
    Jun 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QImage & Pixel manipulation

    Hi guys,

    I'm italian and my english is not so good so...I'll try as possible in a very schematic way!

    1. What I want

    I have an atoms square box L*L (physics simulation) and each of them have two possible spin values (binary information, very simple). I want to display this box to an image (atom ---> pixel, spin value ----->pixel color ).
    So, I initialize my box and display it to my MainWindow. But during the simulation, in each step of my loop I select a spin (a pixel) and I flip it (flip pixel's color).
    It's all! (simply)

    2. What I've done...

    The first algorithm works fine (I can view my image in a correct way)....

    Qt Code:
    1. /* Previously, I declared
    2.  
    3. QImage *reticolo_image=new QImage(L,L,QImage::Format_RGB32);
    4. QPixmap *pix=new QPixmap;
    5. pix->fromImage(*reticolo_image); */
    6.  
    7.  
    8. void MainWindow::inizializzaImage(int *reticolo)
    9.  
    10. /* reticolo is a Pointer to array representing my box..I don't use a matrix
    11. representation, it's the same. */
    12. {
    13. QRgb downValue, upValue;
    14. downValue=qRgb(122,163,39); //green
    15. upValue=qRgb(237,187,51); //orange
    16.  
    17. int t;
    18. for(t=0;t<L*L;t++)
    19. {
    20. if (t==0)
    21. {
    22. if (reticolo[t]==1) reticolo_image->setPixel(0,0,upValue);
    23. if (reticolo[t]==-1) reticolo_image->setPixel(0,0,downValue);
    24. }
    25. else
    26. {
    27. if (reticolo[t]==1) reticolo_image->setPixel(t%L,(int)(t/L),upValue); // t%L=y t/L=x in matricial representation
    28. if (reticolo[t]==-1) reticolo_image->setPixel(t%L,(int)(t/L),downValue);
    29. }
    30. }
    31.  
    32. reticolo_image->scaled(QSize(270, 270));
    33. QPixmap *pixtmp=new QPixmap(270,270);
    34. pixtmp->fromImage(*reticolo_image);
    35. pix=pixtmp;
    36. ui->labelImage->setPixmap(*pix);
    37.  
    38. ui->labelImage->show();
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that I can't upload or view any pixel color change on the screen. I thing that this is a correct (and simple) way to approach this question, instead using QPainter and similar.
    I've simply changed a pixel color with setPixel without using tableColor because I selected RGB32 format.

    Qt Code:
    1. void MainWindow::changeSpin(int spin, int value)
    2. {
    3. int x,y, L;
    4. QRgb downValue, upValue;
    5. downValue=qRgb(122,163,39); //green
    6. upValue=qRgb(237,187,51); //orange
    7.  
    8. L=ui->sL->value();
    9. x=spin%L;
    10. y=(int)(spin/L);
    11. if (value==1) reticolo_image->setPixel(x,y,downValue);
    12. if (value==-1) reticolo_image->setPixel(x,y,upValue);
    13.  
    14.  
    15. ui->labelImage->setPixmap(*pix); // I've also tried without replacing these last rows
    16. ui->labelImage->show();
    17. }
    To copy to clipboard, switch view to plain text mode 

    PS: the second algorithm is a SLOT called by a SIGNAL emitted in my main simulation function.

    I hope someone help me...I know it!

    You are a great resource...
    Thanks
    Attached Files Attached Files
    • File Type: cpp 1.cpp (1.1 KB, 1 views)
    • File Type: cpp 2.cpp (565 Bytes, 1 views)
    Last edited by lycos; 29th June 2013 at 17:33.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage & Pixel manipulation

    You need to convert the changed image to the pixmap again and set it on the label.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage & Pixel manipulation

    Ok, thanks for the answer!

    now I can see changes but... the process is too slow. For each step I have to convert the entire image into a pixmap and not only one pixel.
    Is there any way to set my first pixel configuration as background and change only one of them? (I'm talking about computational time...)

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage & Pixel manipulation

    You can open a painter on the pixmap and overpaint what you want using QPainter API.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Simple form with pixel manipulation
    By mohsenaria in forum Newbie
    Replies: 1
    Last Post: 24th December 2011, 16:30
  2. QImage - Get the RGB component of a certain pixel
    By fabietto in forum Qt Programming
    Replies: 3
    Last Post: 1st July 2011, 16:40
  3. Replies: 2
    Last Post: 10th June 2011, 14:16
  4. Replies: 3
    Last Post: 12th November 2010, 17:08
  5. Shifting a QImage up by 1 pixel row
    By MSUdom5 in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2010, 10:25

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.