0

OS::win xp sp3

Qt::4.6

I am working on the PUZZLE game based on Qt framework and need some help.

In project is included main picture which needs to be disassembled to smaller parts (polygons with images belongig to them). So I make image QImage image = QImage("someImage.jpg") and want to operate with something like croping image on that object.

I checked QImage, QPixmap... docu on nokia site looking for member func. similar to croping but found zero.

Need reference to class I must use ( #include <???> ) to solve this problem and then I will recheck online docu.

Jérôme
  • 26,567
  • 29
  • 98
  • 120
nik_02
  • 53
  • 1
  • 6

3 Answers3

1

Supposed the original image is called A. You have a polygon P. You want to create a smaller image B which is basically A "cropped" by P. Here are the steps:

  • Create B. To know the size, just use QPolygonF::boundingRect on P.
  • Fill B with transparent color or whatever background color you want to have.
  • Creates a new QPainter that works on B
  • Sets the painter clip path to P (polygon can be converted path), see QPainter::setClipPath
  • Draws A using the painter

Note that you might translate the painter if the bounding rect of P is not in the origin.

Ariya Hidayat
  • 12,523
  • 3
  • 46
  • 39
0

You may use these members of QImage

QImage  copy ( const QRect & rectangle = QRect() ) const
QImage  copy ( int x, int y, int width, int height ) const
Raiv
  • 5,731
  • 1
  • 33
  • 51
  • 1
    That is OK for rectangles but I need polygons. – nik_02 Dec 23 '10 at 11:46
  • hmmm... then you may use QRegion, but it has no methods for extracting images parts. Some ugly code like dot-by-dot checking if qregion contains image coordinate may be written if you find nothing better... – Raiv Dec 23 '10 at 11:58
  • Yes , it seems that there is no direct method for extracting poly-image from image.It must be done indirectly but i need some guidelines for furher action. – nik_02 Dec 23 '10 at 12:02
  • Dot by dot is too expensive && unusual , must be easier way. – nik_02 Dec 23 '10 at 12:06
  • and what format you need on exit? something like rectangle with 100% opaque non-used areas? – Raiv Dec 23 '10 at 12:34
  • Need polygon with 100% opaque non-used areas. – nik_02 Dec 23 '10 at 12:41
  • There is no such in-memory structure "polygon". There is image, which is rectangle, there is polygon, which is no more than set of points containing its edges. So you may work with polygons without picture, and only when you draw images get them... if all data within QImage, you may do so: 1) create Qregion from polygon. 2) call QRegion::getBounding rect to get image rectangle 3) call QImage::Copy to obtain that image part 4) Call QPainter::setClipRegion() 5) draw your image I haven't checked this, but i think it will work – Raiv Dec 23 '10 at 16:22
0

You should use a QPixMap: With it you can copy all or parts of the original image into your puzzle parts. By painting into these parts with a transparent color you can make the outside of your polyongs invisible. Or you use bitBlt (Qt 3 Support Member) with QImage for the same effect.

hmuelner
  • 8,093
  • 1
  • 28
  • 39