Qt 按钮上的文本需要多少 space

Qt button take as much space as the text on it needs

我在 Qt Designer 中有一个按钮,我希望它总是占用 space 它上面的文本所需要的数量。我怎样才能做到这一点?

我的意思是按钮上的文字是 "Hello" 而按钮将是 45px。

当我执行 self.button.setText("Hello World") 时,按钮将为 85px。

我现在的 "servers.ui":

<item>
<widget class="QPushButton" name="pageLabel">
 <property name="sizePolicy">
  <sizepolicy hsizetype="Maximum" vsizetype="Preferred">
   <horstretch>0</horstretch>
   <verstretch>0</verstretch>
  </sizepolicy>
 </property>
 <property name="maximumSize">
  <size>
   <width>40</width>
   <height>16777215</height>
  </size>
 </property>
 <property name="text">
  <string/>
 </property>
 <property name="flat">
  <bool>true</bool>
 </property>
</widget>
</item>

我实际上是在说:

使用 QToolButton 而不是 QPushButton 并清除其最大大小。

您可能需要将垂直大小策略设置为 "Preferred",以便它与其他按钮具有相同的高度。要获得平面按钮,请选中 "autoRaise" 属性。

更改按钮 class 的一种快速方法是右键单击它,然后从上下文菜单中 select "Morph Into"。