Results 1 to 2 of 2

Thread: Putting Objects all around the QGraphicsPixmapItem

  1. #1
    Join Date
    Sep 2019
    Qt products

    Question Putting Objects all around the QGraphicsPixmapItem

    Hello !!!

    So i have an island which is a QGraphicsPixmapItem and it looks like this :


    And what i'm trying to do is putting rocks all around the island, but i don't know how to do that ( i know how to put them on the scene i just don't know how to position them around the island )... so i was wondering if there is any kind of a way in which i could do this or do i have to figure out an algorithm on how to go all around the island ?

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 690 Times in 680 Posts
    Qt products
    Windows Android

    Default Re: Putting Objects all around the QGraphicsPixmapItem

    I assume your "island" is actually a rectangular image that you display using a QGraphicsPixmapItem. So if you call any of the methods that return the boundaries (like boundingRect() or shape()) that's just going to return a rectangle. I am guessing that you have already discovered this.

    If there is a clear distinction between "land" and "sea" in your island images, you could write some code to determine a QPainterPath that does represent the outline of the model. A generally simple way to do this is to start at one edge of the pixmap and move towards the center. When the pixel value changes from "sea" to "land", you know you have landed on shore and can then begin walking around the island. Your current pixel location is in the center of a 9-membered square (center plus up, down, left, right, and the diagonals between them). You know that behind you is sea and ahead of you is probably land, so go left and check the pixel "above" you. If it is still land, check the pixels around you to make sure you aren't marching further inland but are sticking to shore (one or more of the pixels must always be sea). Make a line segment from the first and current position, add it to the path, and move on. Eventually, if you keep moving left you will end up back where you started and your QPainterPath will represent the actual outline of the island. You can then map the pixel positions in the path to scene coordinates and place your rocks accurately.

    If you wanted to, you could derive a IslandPixmapItem from QGraphicsPixmapItem and re-implement the shape() method to return this path. That would make it easier to do other things, like determine when your ship has run aground.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 24th June 2016, 10:26
  2. putting ads in app by Qt on android(Qt5.2)
    By stereoMatching in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 27th January 2014, 08:58
  3. Replies: 3
    Last Post: 9th January 2010, 15:47
  4. Replies: 7
    Last Post: 18th July 2006, 21:33
  5. putting widgets
    By mickey in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2006, 08:49

Tags for this Thread


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.