Question


I would like to implement a "grid view" of pixmaps. This is how I would like the UI to behave: You click a button and it shows a QGraphicsView with a QGraphicsScene (done) and then I would like to show all of my QPixmaps in a grid view. I don't actually want to see a grid I just want to organize the pixmaps like 10 columns (pixmaps) pr. row, and then a 10px whitespace in-between each pixmap. (not done). How would this be implemented? EDIT: Here's what I've done so far (which produces the outcome described in the second comment)

public SpriteScene() {
    super(0, 0, 800, 500);

    QPixmap[] sprites = GUI.getWInterface().sprites;
    List<QPixmap> l = Arrays.asList(sprites);
    Iterator<QPixmap> i = l.iterator();
    int rows = 10 / sprites.length;
    boolean isDone = false;

    for(int y = 0; y < rows; y++) {
        for(int x = 0; x < 10; x++) {
            if(i.hasNext()) {
                QGraphicsPixmapItem pixmap = addPixmap(i.next());

                pixmap.setPos(x * 64 + 10 , y * 64 + 10);
            } else {
                isDone = true;
                break;
            }
        }

        if(isDone) {
            break;
        }
    }
}

SpriteScene extends QGraphicsScene and is being added to a QGraphicsView like this:

spriteView = new QGraphicsView(new SpriteScene(), this);
spriteView.setGeometry(0, 35, 850, 550);
spriteView.setAlignment(new Qt.AlignmentFlag[]{Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignTop});
spriteView.hide();


Oh and by the way each pixmap is 64x64px :)

Was it helpful?

Solution

pixmap.setPos(x * 64 + 10 , y * 64 + 10);

Write that down on paper for the first few values:

x = 0, y = 0 => pos = ( 10, 10)
x = 1, y = 0 => pos = ( 74, 10)
x = 2, y = 0 => pos = (138, 10)

There's only 64 pixel different between each successive x offset. You need 74 pixels - the size of the pixmap plus the size of the border.

Set a few variables for your image with, height, horizontal and vertical spacing, and your code should look like:

pixmap.setPos(x * (width+hspacing) + offsetx, y * (height+vspacing) + offsety);

The offsetx/y would probably look nicer if they were half the respective spacing valued to get the grid "centered".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top