简体   繁体   English

Qt4 Designer使用QTableWidget创建对话框

[英]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. 我有一个简单的问题,在Qt Designer中使用Qt v4.7.1使用QTableWidget。 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. 我想创建一个QTableWidget设置为3x3网格的对话框,然后设置表的初始大小以准确显示3x3网格,没有滚动条。 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: 我在Qt Designer中完成了这些步骤:

  1. Create a new dialog (for this example we'll use a dialog without buttons). 创建一个新对话框(对于此示例,我们将使用没有按钮的对话框)。
  2. Drag in a QTableWidget. 在QTableWidget中拖动。
  3. Double click on the table and set 3 rows (called 1, 2 and 3) and 3 columns (likewise) then hit OK. 双击该表并设置3行(称为1,2和3)和3列(同样),然后单击“确定”。
    • 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? 是否可以在Qt Designer中执行此操作,还是应该在代码中设置大小?

Thanks a lot for any replies. 非常感谢任何回复。

Note that unless the QTableWidget doesn't force any size, neither does your QDialog. 请注意,除非QTableWidget不强制任何大小,否则QDialog也不会。 So what you need is to fix the size of the QTableWidget; 所以你需要的是修复QTableWidget的大小; the QDialog doesn't have anything to do with this. QDialog与此无关。 The layout then will try to resize the QDialog when the QTableWidget wants to force another size. 当QTableWidget想要强制另一个大小时,布局将尝试调整QDialog的大小。

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. 但是据我所知,将QTableWidget的大小设置为特定的行数/列数是不可能的,至少在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(...) . 然后使用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): 对于固定单元大小和不可见标头,有一种可能的解决方案,没有手动代码(完全在QtDesigner中):

  • Set (horizontal|vertical)HeaderVisible = false 设置(水平|垂直)HeaderVisible = false
  • Set (horizontal|vertical)HeaderDefaultSectionSize = (fixedValue) 设置(水平|垂直)HeaderDefaultSectionSize =(fixedValue)
  • Set (horizontal|vertical)HeaderMinimumSectionSize = (fixedValue) 设置(水平|垂直)HeaderMinimumSectionSize =(fixedValue)
  • Set the size of the QTableWidget to the sum of the sizes. 将QTableWidget的大小设置为大小的总和。 Remember to add (column count + 1) and (row count + 1) pixels to your fixed total size of the QTableWidget. 请记住将(列数+ 1)和(行数+ 1)像素添加到QTableWidget的固定总大小。

For fixed cell size you can use setFixedSize and QSizePolicy::Fixed directly from QtDesigner. 对于固定单元格大小,您可以直接从QtDesigner使用setFixedSize和QSizePolicy :: Fixed。

If you want resize column/row for it's content length - use resizeColumn(s)[Row(s)]ToContents in code. 如果要为其内容长度调整列/行的大小 - 在代码中使用resizeColumn(s)[Row(s)] ToContents。

If you want attach resize to QDialog resizing - write code in resizeEvent of Dialog in code. 如果你想将调整大小附加到QDialog调整大小 - 在代码中的Dialog的resizeEvent中编写代码。

Dinamically resizing table cell is able only from code not from QtDesigner 动态调整表格单元格仅能从代码中获取,而不能来自QtDesigner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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