Results 1 to 6 of 6

Thread: QImage scaled twice to same instance and QPainter paint abnormally

  1. #1
    Join Date
    Jun 2010
    Location
    Hangzhou, China
    Posts
    10
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Question QImage scaled twice to same instance and QPainter paint abnormally

    I confused about line 15.
    If commented it, the code will create a pixmap filled with many circle. That is named resultA.
    If it works, the code creates a pixmap that contents is balloon + circle. That is named resultB.
    Why?
    I think the painter variable is not relevant with QImage instances tmpdi and image, just only relevant displayImage instance.
    Can anyone give me any suggestions to explain it?
    Thanks!
    Qt Code:
    1. int main (int argc, char** argv)
    2. {
    3. QSize resultSize(281,377);
    4. QSize pegSize(21,29);
    5.  
    6. QImage image;
    7. image.load("balloon.jpg");
    8. QImage tmpdi;
    9. tmpdi = image.scaled(resultSize,Qt::KeepAspectRatio);
    10. // tmpdi = image.scaled(QSize(resultSize.width()*1.5, resultSize.height()*2.0),Qt::KeepAspectRatio);
    11.  
    12. QImage displayImage= QImage(resultSize,QImage::Format_ARGB32_Premultiplied);
    13. QPainter painter1(&displayImage);
    14. painter1.fillRect(QRect(0,0,resultSize.width(),resultSize.height()), Qt::transparent);
    15.  
    16. float ratioPegX = resultSize.width() * 1.0 / pegSize.width();
    17. float ratioPegY = resultSize.height() * 1.0 / pegSize.height();
    18.  
    19. //draw circle on it
    20. for (int pegi = 0; pegi < pegSize.width(); pegi++)
    21. for (int pegj = 0; pegj <pegSize.height(); pegj++)
    22. {
    23. QRectF rectf = QRectF( pegi * ratioPegX, pegj * ratioPegY,
    24. ratioPegX, ratioPegY);
    25.  
    26. painter1.drawEllipse(rectf.center(), ratioPegX/2.0, ratioPegY/2.0);
    27. }
    28.  
    29. displayImage.save("x3.png");
    30.  
    31. return 0;
    32. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage scaled twice to same instance and QPainter paint abnormally

    Hi!

    There is no line 15 in your code. There is no resultA and no resultB. Please rephrase your problem and question!

    Johannes

  3. #3
    Join Date
    Jun 2010
    Location
    Hangzhou, China
    Posts
    10
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage scaled twice to same instance and QPainter paint abnormally

    I am so sorry the line number is wrong, it should be line 10.
    Thank you, Johannes.
    Now I upload the test image balloon.jpg and test result image resultA.png and resultB.png.
    Attached Images Attached Images

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

    Default Re: QImage scaled twice to same instance and QPainter paint abnormally

    The painter works on an object you point it to. It has no influence on other objects (be it images or anything else). I don't know if that answers your question... If not then maybe try rephrasing 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.


  5. #5
    Join Date
    Jun 2010
    Location
    Hangzhou, China
    Posts
    10
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage scaled twice to same instance and QPainter paint abnormally

    Quote Originally Posted by wysota View Post
    The painter works on an object you point it to. It has no influence on other objects (be it images or anything else).
    I think so too. Just the comprehension make me confused with the code I pasted.

    Let me describe it in detail.
    line 10 is commented, compile ->run it will create 1st image of my uploaded files.
    line 10 is NOT commented, compile->run, it will create 2nd image.

    balloon.jpg in line 10 is the 3rd image.

    I only scaled an image and then return itself.


    Added after 8 minutes:

    Another phenomenon:
    if the line 8 ~ line 10 such as below.
    "
    QImage tmpdi;
    tmpdi = image.scaled(resultSize,Qt::KeepAspectRatio);
    tmpdi = image.scaled(QSize(resultSize.width()*1.5, resultSize.height()*2.0),Qt::KeepAspectRatio);
    "
    run it, will get 2nd image of my uploaded image files.
    if it changed as
    "
    QImage tmpdi;
    QImage tmpditmp = image.scaled(resultSize,Qt::KeepAspectRatio);
    tmpdi = image.scaled(QSize(resultSize.width()*1.5, resultSize.height()*2.0),Qt::KeepAspectRatio);
    "
    it will create 1st image.

    I do not know why it gets different result? I only do create a new instance.
    I Can NOT do scale operation on same object, can I?
    If I do, what happen on Painter?

    or the code has some defects?
    Last edited by oliverliu.hz; 12th February 2011 at 08:24.

  6. #6
    Join Date
    Jun 2010
    Location
    Hangzhou, China
    Posts
    10
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage scaled twice to same instance and QPainter paint abnormally

    Can one tell me the reason?

Similar Threads

  1. QPainter::begin: Cannot paint on a null pixmap
    By sabeesh in forum Qt Programming
    Replies: 5
    Last Post: 27th July 2010, 18:03
  2. How to paint (QPainter) on a label ?
    By d@nyal in forum Newbie
    Replies: 1
    Last Post: 29th May 2010, 18:39
  3. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  4. QImage::scaled takes long time
    By nrabara in forum Qt Programming
    Replies: 0
    Last Post: 15th December 2009, 12:19
  5. QImage::scaled() error in Qt4/e
    By Neo in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2006, 06:15

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
  •  
Qt is a trademark of The Qt Company.