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:
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?
Thanks a lot for any replies.
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):