Results 1 to 2 of 2

Thread: "Render" into QImage

  1. #1
    Join Date
    Sep 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default "Render" into QImage

    Hi all,

    i have a little probelm. I wanted to write a renderer and use qt as a frontend. Now my question is, how do i display and update my image _efficently_. As the rendering process needs time there are only a few pixels which gets updated in a certain interval.

    I've written a little program which shows how i would solve this problem. But i'm not very happy with this solution because i don't know how much copying of the image is done under the hood. Maybe theres a better way?

    Here the code:

    Qt Code:
    1. class QMainWin : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. QMainWin();
    7.  
    8. private:
    9. QScrollArea *area;
    10. QImage *image;
    11. QLabel *imgLabel;
    12. int x, y;
    13.  
    14. private slots:
    15. void drawPixels();
    16. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QMainWin::QMainWin()
    2. {
    3. image = new QImage(200, 200, QImage::Format_ARGB32);
    4. imgLabel = new QLabel;
    5. imgLabel->setPixmap(QPixmap::fromImage(*image));
    6.  
    7. area = new QScrollArea;
    8. area->setBackgroundRole(QPalette::Dark);
    9. area->setWidget(imgLabel);
    10.  
    11. setCentralWidget(area);
    12.  
    13. QAction* action = new QAction(tr("Draw something"), this);
    14. connect(action, SIGNAL(triggered()), this, SLOT(drawPixels()));
    15.  
    16. QMenu* menu = menuBar()->addMenu(tr("Drawing"));
    17. menu->addAction(startAction);
    18.  
    19. x = y = 0;
    20. }
    21.  
    22. void QMainWin::drawPixels()
    23. {
    24. for(int i = y; i < y+20; i++)
    25. for(int j = x; j < x+20; j++)
    26. image->setPixel(j, i, qRgb(255, 0, 0));
    27.  
    28. x += 20;
    29. y += 20;
    30.  
    31. // This line is needed, else the picture doesn't get updated
    32. imgLabel->setPixmap(QPixmap::fromImage(*image));
    33. imgLabel->update();
    34. }
    To copy to clipboard, switch view to plain text mode 

    I've omitted forward declarations, includes and so on.
    Sry for the bad english.

    Regards
    Imons

  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: "Render" into QImage

    What exactly are you not satisfied with? QImage (and QPixmap as well) is an implicitly shared class. If you copy an object to a new one only a shallow copy is performed but when you change at least one bit of information of any of the incarnations, a deep copy of the whole image is made for the instance being changed.

    Anyway, the loop where you do the painting can be replaced by a single call to QPainter::drawRect(). Of course you need to modify the way you operate on QPixmap but then you can get rid of QImage at all.
    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. Replies: 3
    Last Post: 15th February 2010, 17:27
  2. QImage and "raw" grey scale
    By high_flyer in forum Qt Programming
    Replies: 9
    Last Post: 9th December 2008, 17:01
  3. Replies: 3
    Last Post: 8th July 2008, 19:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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.