Results 1 to 3 of 3

Thread: How to copy pixmap and keep scale ratio?

  1. #1
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default How to copy pixmap and keep scale ratio?

    I need to copy portion of pixmap and fill into onscreen painter.

    my code that not work
    Qt Code:
    1. painter.drawPixmap(pixMap->rect(),*pixMap,recvMap);
    To copy to clipboard, switch view to plain text mode 

    the problem is image on screen is not correctly scale like original one
    seem it ratio is enlarge to fit target rect.

    I just need raw portion of pixmap and scale that keep image ratio.

    Thank.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to copy pixmap and keep scale ratio?

    Use QPixmap::scaled() with Qt::KeepAspectRatio

  3. #3
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to copy pixmap and keep scale ratio?

    I tried but seem not got result i expected.

    Portion of image not scale to full screen render area.


    I copy portion of pixmap into another pixmap buffer
    then make it scale

    look at my code

    Qt Code:
    1. QPixmap buffer(pixMap->rect().width(),pixMap->height() );
    2. QPainter painterBuffer(&buffer);
    3. QPainter painter(this);
    4.  
    5. //painter.scale(mapScale,mapScale);
    6.  
    7. int cX = pixMap->width()/2;
    8. int cZ = pixMap->height()/2;
    9.  
    10.  
    11. //myPlayer.curPos.x = myX;
    12. //myPlayer.curPos.z = myZ;
    13. int cMapX = pixMap->width()/2;
    14. int cMapY = pixMap->height()/2;
    15.  
    16.  
    17.  
    18. int gamePointX=0;
    19. int gamePointZ=0;
    20.  
    21. QRect recvMap;
    22. recvMap.setLeft( gamePointX + cMapX + (myX - cX/mapScale)/scale );
    23. recvMap.setRight( gamePointX + cMapX + (myX + cX/mapScale)/scale);
    24. recvMap.setTop( gamePointZ + cMapY - (myZ + cZ/mapScale)/scale);
    25. recvMap.setBottom( gamePointZ + cMapY - (myZ - cZ/mapScale)/scale);
    26.  
    27. painter.setRenderHint(QPainter::Antialiasing,true);
    28. QRectF srcRect=pixMap->rect();
    29.  
    30. painter.fillRect(rect(),QBrush(Qt::black));
    31.  
    32.  
    33.  
    34. painterBuffer.drawPixmap(QPointF(1,1),*pixMap,recvMap);
    35.  
    36. buffer.scaled(512,512,Qt::KeepAspectRatio);
    37. painter.save();
    38.  
    39. painter.drawPixmap(QPointF(1,1),buffer,buffer.rect());
    40.  
    41. painter.restore();
    42.  
    43. painter.setPen(Qt::white);
    44.  
    45. QFont font(font());
    46. font.setPointSize(24);
    47. painter.setFont(font);
    48.  
    49. QFontMetrics fm(painter.font());
    50. int fontHeigh = fm.height();
    51. QString tmp;
    52. tmp.sprintf("%.0f %.0f",mouseMovePos.x(),mouseMovePos.y());
    53. //painter.drawText(0,fontHeigh,tr("%1,%2").arg(mouseMovePos.x()).arg(mouseMovePos.y()));
    54. painter.drawText(0,fontHeigh,tmp);
    55. tmp.clear();
    56. tmp.sprintf("My Pos %d %d",myX,myZ);
    57. painter.drawText(0,fontHeigh*2,tmp);
    To copy to clipboard, switch view to plain text mode 
    result image
    Attached Images Attached Images

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.