Qt4 Designer para crear un diálogo con un QTableWidget

I have what is hopefully a simple problem using a QTableWidget in Qt Designer with Qt v4.7.1. I would like to create a dialog with a QTableWidget set to a 3x3 grid, then set the inititial size of the table to exactly show the 3x3 grid, without scrollbars. I don't want the table to be any bigger or smaller than this. I then want to set the initial size of the dialog (it can be a fixed size) to the exact size required for this table.

I have gone through these steps in Qt Designer:

  1. Create a new dialog (for this example we'll use a dialog without buttons).
  2. Drag in a QTableWidget.
  3. Double click on the table and set 3 rows (called 1, 2 and 3) and 3 columns (likewise) then hit OK.
    • The table is now the same size as the empty table when I initially dragged it in. This isn't wide enough for three columns, so I have a horizontal scrollbar, and is too high for three rows so I have a lot of empty space at the bottom.
  4. Right-click on the dialog and select "Lay Out In A Grid" (or horizontally/vertically).
    • The table now resizes to fill the dialog which is larger than required, so I can see all rows/columns but there's lots of empty space on the right & bottom.
  5. Right-click on the dialog and say "Adjust Size".
    • The table now jumps back to the initial size, ie. not wide enough so with a horizontal scroll bar, and too high. The dialog is resized to fit this table.

The dialog resizing to fit the table is correct. But how can I resize the table to fit it's contents?

I have tried lots of other things, eg. changing the size policy on the table to "fixed,fixed" or "minimum,minimum" but it still wants to set the initial size to a size that doesn't match the contents. Ideally I would like the table/dialog to set only the correct initial size and allow the user to make the dialog smaller (which would add scrollbars) but it's not the end of the world if I have to make the size fixed.

Is it possible to do this in Qt Designer or should I be setting the size in code?

Muchas gracias por cualquier respuesta.

preguntado el 22 de mayo de 12 a las 12:05

2 Respuestas

Note that unless the QTableWidget doesn't force any size, neither does your QDialog. So what you need is to fix the size of the QTableWidget; the QDialog doesn't have anything to do with this. The layout then will try to resize the QDialog when the QTableWidget wants to force another size.

But setting the size of a QTableWidget to a specific count of rows/cols isn't possible as far as I know, at least not from within QtDesigner.

You need to write a bit of code to achieve this. The code needs to request the widths of the columns and the heights of the rows (including headers), and add the borders of the widget. Then set this size using ui->tableWidget->setFixedSize(...). You would then also need to react on resizing of the columns / rows, if you enable this for the user.

For fixed cell size and invisible headers, there is a possible solution with no manual code (fully in QtDesigner):

  • Set (horizontal|vertical)HeaderVisible = false
  • Set (horizontal|vertical)HeaderDefaultSectionSize = (fixedValue)
  • Set (horizontal|vertical)HeaderMinimumSectionSize = (fixedValue)
  • Set the size of the QTableWidget to the sum of the sizes. Remember to add (column count + 1) and (row count + 1) pixels to your fixed total size of the QTableWidget.

contestado el 22 de mayo de 12 a las 13:05

Thanks a lot for the reply. I did something similar to this the last time I came across this issue. But I would like to know if it's possible to achieve it in Designer. It seems like such an obvious thing and something that I'm sure other people have come across. I can do it in code, but that kinda removes the advantage of Designer as a tool to show what the dialog will look like. My real dialog is a bit more complicated than this example and includes a couple of 3x3 tables among other widgets so I was hoping to lay it out in Designer. - giles123

What you can do is enabling [horizontal|vertical]HeaderStretchLastSection. But this would be the other way around: The cells would then adapt its size to the tree widget / dialog. - lemes

I would like all of the cells to be the same size though. It's a 3x3 matrix of coefficients so it doesn't make sense for some cells to be different sizes. Also, as well as making the third column small, it would make the third row huge as the default table size is roughly twice as tall as three rows. On the subject of cell sizes, is it possible to resize cells in Designer or is that something else that has to be done in code? - giles123

@giles123 This is possible. I updated my answer; see the list at the bottom. If you want headers (which I doubt in such a small matrix editor), I don't know how to force their size. - lemes

Thanks Leemee. For this case, with no headers, I think your answer is a good solution. However, I was originally thinking that you would be able to achieve this quite simply without specifying column sizes. I thought there would be a way to add a table, set the number of rows and columns, and then automatically set the size of the table (and therefore dialog) to the appropriate size. However, it appears that it isn't quite as easy as this. For my current purpose, your solution will work and I will accept it. And at least now I know I wasn't missing something obvious. Thanks. - giles123

For fixed cell size you can use setFixedSize and QSizePolicy::Fixed directly from QtDesigner.

If you want resize column/row for it's content length - use resizeColumn(s)[Row(s)]ToContents in code.

If you want attach resize to QDialog resizing - write code in resizeEvent of Dialog in code.

Dinamically resizing table cell is able only from code not from QtDesigner

contestado el 22 de mayo de 12 a las 13:05

Thanks for the reply Alexeev. It seems that in the more general of my original question, the answer is that it is much easier to do at least some of it in code, and probably just implement the whole table in code. However, in my specific case leemes answer fits the bill. - giles123

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.