QGraphicsScene 像素图网格视图

发布于 2024-12-19 10:48:24 字数 1265 浏览 1 评论 0原文


我想实现像素图的“网格视图”。这就是我希望 UI 的行为方式:单击一个按钮,它会显示一个带有 QGraphicsScene 的 QGraphicsView(完成),然后我想在网格视图中显示我的所有 QPixmap。我实际上不想看到网格,我只想组织像素图,例如 10 列(像素图)。行,然后每个像素图之间有 10 像素的空白。 (未完成)。这将如何实施? 编辑:这是我到目前为止所做的(产生第二条评论中描述的结果)

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 扩展了 QGraphicsScene 并被添加到 QGraphicsView 中,如下所示:

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();


哦,顺便说一下每个像素图是 64x64 像素:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾北清歌寒 2024-12-26 10:48:24
pixmap.setPos(x * 64 + 10 , y * 64 + 10);

将前几个值写在纸上:

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

每个连续的 x 偏移之间仅存在 64 像素的差异。您需要 74 像素 - 像素图的大小加上边框的大小。

为图像设置一些变量,包括高度、水平和垂直间距,您的代码应如下所示:

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

如果 Offsetx/y 是获取网格的相应间距值的一半,那么它们可能看起来会更好“居中”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文