【问题标题】:Qt button take as much space as the text on it needsQt 按钮占用的空间与它所需要的文本一样多
【发布时间】:2017-03-21 05:06:26
【问题描述】:

我在 Qt 设计器中有一个按钮,我希望它始终占用与文本所需的空间一样多的空间。我怎样才能做到这一点?

我的意思是按钮上的文字是“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>

其实我说的是:

【问题讨论】:

标签: qt layout pyqt qt-designer


【解决方案1】:

使用QToolButton 代替QPushButton 并清除其最大大小。

您可能需要将垂直尺寸策略设置为“首选”,以便它与其他按钮具有相同的高度。要获得扁平按钮,请检查“autoRaise”属性。

更改按钮类的快速方法是右键单击它并从上下文菜单中选择“Morph Into”。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-01
  • 1970-01-01
  • 2014-08-26
相关资源
最近更新 更多