Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to cut an image into small (not rectangular shaped) parts?
Qt 6.12 LTS is out! See what's new in the release blog

How to cut an image into small (not rectangular shaped) parts?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    justAnoob
    wrote on last edited by
    #1

    Basically i want to make a little puzzle game, therefore i need to take an image and cut it into little puzzle parts.
    Now the problem is i have no idea how to achieve this and need some hints into the right direction or ultimately a little code example.

    thanks in advance

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Calculate the rectangle of the little piece and then use "copy":http://qt-project.org/doc/qt-5.0/qtgui/qpixmap.html#copy

      1 Reply Last reply
      0
      • J Offline
        J Offline
        justAnoob
        wrote on last edited by
        #3

        [quote author="Chris Kawa" date="1368906652"]Calculate the rectangle of the little piece and then use "copy":http://qt-project.org/doc/qt-5.0/qtgui/qpixmap.html#copy[/quote]

        With copy i can only get rectangles (or am i missing something?), thats why i asked for a way to get custom shapes out of an image.

        I want to split an image into shapes like this:
        !http://c.heimwerker.de/fa/heimwerker-content/thema/wandgestaltung/techniken/schablone/puzzleteil_2.gif(puzzle)!

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          It's still a rectangle image. You just need to apply an alpha channel (transparency) to it.
          Take a look at "setMask":http://qt-project.org/doc/qt-5.0/qtgui/qpixmap.html#setMask
          You can use the image above for the mask, just need to invert the colors. Black is transparent, white opaque.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Yeah, the other solution would be to have the puzzle piece a vector shape with the image being implemented as a fill, but it will be harder to do compared to images.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              justAnoob
              wrote on last edited by
              #6

              First of all thanks for the answers.
              When i think about using setMask im running into a difficulty in my brain. I guess its possible to do it with masks, but of course not every puzzle piece should look the same. In order to give them a "random" look (like the "nipple" more to the top or to the bottom of the piece or the hole more left/right etc.) i would need to use alot of different masks while making sure they are still compatible with eachother (to complete the puzzle in the end).

              So is there maybe another way?

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Just use a collection of images for different pieces. You can even write a function to generate the rectangle from the mask up left top and bottom maximum values and divide up your image into the right size bits with appropriate masks. I guess it will be a little more work to figure where each piece snaps into place, but certainly doable.

                But I guess the best way to go is to actually randomly generate your puzzle pieces. Start with a square, and depending on its position in the entire puzzle just add 2-4 shapes to each end of the square, and naturally, reuse that same shape's negative for the pieces that are situated next.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved