gpt4 book ai didi

java - QGraphicsScene 像素图 GridView

转载 作者:行者123 更新时间:2023-12-01 15:39:04 25 4
gpt4 key购买 nike


我想实现像素图的“ GridView ”。这就是我希望 UI 的行为方式:单击一个按钮,它会显示一个带有 QGraphicsScene 的 QGraphicsView(完成),然后我想在 GridView 中显示我的所有 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();


哦,顺便说一句,每个像素图都是 64x64px :)

最佳答案

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 是使网格“居中”的相应间距值的一半,它们可能看起来会更好。

关于java - QGraphicsScene 像素图 GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com