0

I'm currently doing a map editor for a game I created, and I have a tiny problem with Qt. It's the first time I use it, but I love the challenge and I thought it would be a great experience.

I'm currently building my interface using Qt Designer and I'm stuck on the map part. Indeed, my map is composed of 2 layers : The terrain, and the units/obstacles/vegetation.

I'm using a QTableWidget to contain the terrain information, but now I would like to stack another QTableWidget above it, with the exact same size, so I can place my units, etc. However, I didn't find how to achieve this on Qt Designer. As soon as I create a new QTableWidget, it goes on the side of the first, not above it.

I thought about using the QStackedWidget, but since it only allows 1 Widget to be visible at a time, it doesn't fit my hope.

If someone has an idea, as tiny as it might be, I would be very thanksful.

Have a nice day everyone !

EDIT : By above, I don't mean aside... I know it's kinda hard to explain. If we consider that the Axis on which we look our screen is Z, I would like both tables to be : - Equivalent on the X Axis (same number of columns) - Equivalent on the Y Axis (same number of row) - Different on the Z Axis (one table is "deeper")

  • By "stacking", do you mean that they are *one above/below the other*, both being visible, with different y coordinate on the screen, or *one behind the other*, having the same coordinates? – leemes Jul 12 '12 at 09:45
  • Yeah my mistake. I mean behind. Well something like this : http://en.wikipedia.org/wiki/File:Neapolitaner_Waffel_2.jpg Imagine that each chocolate part is actually a QTableWidget. Sorry, I didn't find any other example (in picture) to explain what I really want... And I love waffles ! –  Jul 12 '12 at 09:58
  • If they are behind each other *at the same coordinates*, only one of the widgets can be visible. This can be done using QStackedWidget. But if you want them to overlap only a certain amount, like the waffles on the image, this won't be possible using Qt's layouts. You need to specify how the widgets should behave when the outer widget is resized. How can the user interact with the lower widget? Should the widgets swap their "Z depth" when focusing the widget behind the other? – leemes Jul 12 '12 at 10:02
  • Basically, when the user clicks on the "terrain" action, he will work on the lower Table. When the user clicks on the "unit" action (or vegetation), he will work on the upper Table. However, I would like the terrain to remain visible when the user works on the vegetation... –  Jul 12 '12 at 10:06
  • Okay. But how do you want to lay out the two tables? How much should be visible of the lower widget? Exactly the half, for example? Or exactly 20 pixels on the left/right? (Meaning that the widgets use the full width minus 20 pixels, and the one table is at (0,0) coordinate, the other at (20,0)) – leemes Jul 12 '12 at 11:16
  • They must have the exact same size (rows & columns). The table on top (so the unit/vegetation) should have a transparent background. Basically, I was thinking my map could be split in 2 parts : 1 part on the terrain (grass,sand,whatever), and 1 part for units and vegetation : You need a ground for a soldier to walk, you need a ground for a tree to grow. So I was thinking that making kid of "2-layered" array would be a great idea. The deep one will contain grass, the top one will contain some trees and some units (for instance). Maybe it's not a good idea, but still I can't figure out any other –  Jul 12 '12 at 12:45
  • 1
    Ah, now I got you. I thought you use the table widgets for the library of available map tiles or something similar. You use the table for the map itself! This is not the best idea. First, I don't think that QTableWidget supports transparent background. Second, you should consider using something better suited for such a scene. Take a look at QGraphicsView. It draws the contents of a QGraphicsScene, in which you put QGraphicsItems (QGraphicsPixmapItems for tiles) inside. You have scroll bars and can enable mouse drag n drop for moving around in the scene. – leemes Jul 12 '12 at 13:50
  • Thanks for your comment. Can QGraphicsScene be "grided" too ? And yes, a QTableWidget supports transparent background :) –  Jul 12 '12 at 14:01
  • 1
    You need to do it on your own. You should place the images ("tiles") in the scene using grid based coordinates, so that `QPoint(tilewidth * tilex, tileheight * tiley)` will be the resulting screen coordinate. Do you need to do drag'n'drop within the scene? This can be a bit complicated. But if you only want to snap the mouse to the grid, just do so in the mouse handlers by rounding the mouse coordinates to the next tile coordinate. – leemes Jul 12 '12 at 14:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13795/discussion-between-nnzz-and-leemes) –  Jul 12 '12 at 15:13

1 Answers1

0

Note: This answer is off-topic, as I misunderstood the question. The question is about stacking widgets but painting all of them. However, I leave it there if a future reader wants to put widges one below the other. (See the comments on the questions for details.)


You can put widgets one below the other using QVBoxLayout. Just select the two table widgets and select 'Lay out' > 'Lay out vertically' in the context menu. You should then use row stretches to tell the layout that the widgets should have the same sizes. As far as I know, stretches can only be set using C++ code, not from within your .ui designer file. This can be done by putting these two lines in the constructor of your widget class, right after ui->setup(this);:

ui->verticalLayout->setStretch(0, 50); //  50%, but the number doesn't have to be
ui->verticalLayout->setStretch(1, 50); //  in percent, so 1 / 1 is the same.

If I understand it correctly, you want to have a big widget, your map editor's main widget, and the two other widgets below or at the side. If they are below, you can put this together into one QVBoxLayout and set the stretches, for example, to 3:1:1, which makes the main widget occupy 60% of the available height and your two table widgets 20% each:

ui->verticalLayout->setStretch(0, 3);
ui->verticalLayout->setStretch(1, 1);
ui->verticalLayout->setStretch(2, 1);
leemes
  • 44,967
  • 21
  • 135
  • 183