简体   繁体   中英

Qt4 Designer to create a dialog with a 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?

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):

  • 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM