Results 1 to 7 of 7

Thread: Please help - Refreshing a QPixmap repeatedly in a for loop ( ??? )

  1. #1
    Join Date
    Jun 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Please help - Refreshing a QPixmap repeatedly in a for loop ( ??? )

    Hello Everyone,

    I am manipulating a rectangular array pixel-by-pixel with QImage and setpixel. Next, I convert the QImage to a QPixmap, and display it through a QLabel. This process works pretty well in itself, but, I would like to be able to manipulate the image's pixels over and over, and display the "updated" Qimage (or Qpixmap) file in the label.

    Right now, the QLabel remains white/blank until the for.. loop it is in, finishes. Instead, I'd like to see the changed pixmap after each 'step' within the for loop.

    Any suggestions? Is there a method to successfully output the QImage in my QLabel after each loop through the step loop? The ideal result would be a Qlabel that displays the Qpixmap which, after each time through the for (step<numbstep) loop, the color is slightly modified via adjusting the R G B values. Right now, it only outputs the "final" color. (Code Below):


    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include <QLabel>
    4. #include <iostream>
    5. #include <QImage>
    6. #include <QPixmap>
    7.  
    8.  
    9. using namespace std;
    10.  
    11.  
    12. Widget::Widget(QWidget *parent) :
    13. QWidget(parent),
    14. ui(new Ui::Widget)
    15. {
    16. ui->setupUi(this);
    17.  
    18. mylabel = new QLabel; //
    19. mylabel->show(); //
    20.  
    21. myimage = QImage(100,100,QImage::Format_RGB32);
    22.  
    23. for(int step=0;step<250;step++)
    24. {
    25. da = 3;db=1;dc=.5;
    26.  
    27. for(int i=0;i<100;i++)
    28. {
    29. for(int j=0;j<100;j++)
    30. {
    31.  
    32. QRgb colorval;
    33. colorval = qRgb(a,b,c);
    34.  
    35. myimage.setPixel(j,i,colorval);
    36. //cout << " Setting " << i << " " << j << endl;
    37. }
    38.  
    39. }
    40. a = a + da;
    41. b = b + db;
    42. c = c + dc;
    43.  
    44. QPixmap mypixmap = QPixmap::fromImage(myimage);
    45.  
    46. mylabel->setPixmap(mypixmap);
    47. mylabel->setScaledContents(true);
    48. mylabel->update();
    49.  
    50. }
    51. }
    52.  
    53. Widget::~Widget()
    54. {
    55. delete ui;
    56. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Please help - Refreshing a QPixmap repeatedly in a for loop ( ??? )

    [wiki]Keeping the GUI Responsive[/wiki]
    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 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Please help - Refreshing a QPixmap repeatedly in a for loop ( ??? )

    Okay, thanks, I took a look at your post, and implemented :
    Qt Code:
    1. QCoreApplication::processEvents();
    To copy to clipboard, switch view to plain text mode 
    after setting my pixmap from my Qimage.

    Now, I can visually notice the pixmap being updated with a new color, rapidly.

    Onto another issue, is there a simple way to delay a step by a given amount of ms or seconds? The pixmap is updated a little too rapidly at this point. It would be nice to be able to control the time the pixmap is displayed.

    Right now the only way I can "trick" the program into slowing down is to perform many more steps in my step loop (~20,000 steps) and output my pixmap every 100 steps or so.

    Thanks,
    Last edited by mkmartin06; 16th June 2011 at 20:08.

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

    Default Re: Please help - Refreshing a QPixmap repeatedly in a for loop ( ??? )

    Read the article again. Implement the "step by step" approach, just adjust the timer.
    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.


  5. #5
    Join Date
    Jun 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Please help - Refreshing a QPixmap repeatedly in a for loop ( ??? )

    My example code that I provided in my first post is a greatly simplified case of what actually occurs in my for loop. When I try to implement
    Qt Code:
    1. QCoreApplication::processEvents();
    To copy to clipboard, switch view to plain text mode 
    in my program that does a lot of calculations within the loop, the program only displays the final step's pixmap in the QLabel. The intermediate pixmaps which I need displayed never show up. Any suggestions?
    Last edited by mkmartin06; 16th June 2011 at 20:58. Reason: updated contents

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

    Default Re: Please help - Refreshing a QPixmap repeatedly in a for loop ( ??? )

    Quote Originally Posted by mkmartin06 View Post
    Any suggestions?
    Yes. Don't use processEvents() and redesign your code. Read what the article says on using processEvents(), especially the part starting with "This approach has significant drawbacks". And then read my previous post again and follow the guideline contained in it.
    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.


  7. #7
    Join Date
    Jun 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Please help - Refreshing a QPixmap repeatedly in a for loop ( ??? )

    Ok. That is what I did. It works now. Have a nice day.

Similar Threads

  1. Replies: 0
    Last Post: 15th April 2011, 23:38
  2. play sound repeatedly
    By ardisaz in forum Qt Programming
    Replies: 4
    Last Post: 4th December 2010, 11:10
  3. Qt Creator upgraded to 2.0. Now repeatedly asked about Debugging Helper
    By winkle99 in forum Qt Tools
    Replies: 0
    Last Post: 25th June 2010, 23:16
  4. refreshing QPixmap
    By Wojtek.wk in forum Newbie
    Replies: 1
    Last Post: 18th April 2010, 02:00
  5. Calling QThread::exec() repeatedly
    By hb in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2007, 20:24

Tags for this Thread

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.