2016-11-07 103 views
3

我在Qt Designer中有一個按鈕,我希望它總是佔用與它需要的文本一樣多的空間。我怎麼能做到這一點?Qt按鈕需要佔用足夠的空間作爲它需要的文本

我的意思是像按鈕上的文字是「你好」,按鈕將是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> 

,我實際上是在談論:

https://i.imgur.com/2I47qVR.png

+0

嘗試在更改文本後調用updateGeometry http://doc.qt.io/qt-4.8/qwidget.html#updateGeometry – Chris

+0

像這樣https://i.imgur.com/S5N1JN0.png?它仍然被切斷。 – Bluscream

+0

Designer中的設置:https://i.imgur.com/A5nqzlK.png – Bluscream

回答

2

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

您可能需要將vertical size-policy設置爲「Preferred」,以使其與其他按鈕具有相同的高度。要獲得平面按鈕,請選中「autoRaise」屬性。

更改按鈕類的快捷方式是右鍵單擊它,然後從上下文菜單中選擇「Morph Into」。

+1

我必須將水平尺寸策略設置爲最大,但除此之外; https://i.imgur.com/XXy7D3y.png – Bluscream