QGraphicsScene 像素图网格视图
我想实现像素图的“网格视图”。这就是我希望 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将前几个值写在纸上:
每个连续的
x
偏移之间仅存在 64 像素的差异。您需要 74 像素 - 像素图的大小加上边框的大小。为图像设置一些变量,包括高度、水平和垂直间距,您的代码应如下所示:
如果 Offsetx/y 是获取网格的相应间距值的一半,那么它们可能看起来会更好“居中”。
Write that down on paper for the first few values:
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:
The
offsetx/y
would probably look nicer if they were half the respective spacing valued to get the grid "centered".