Qt:QPushButton 垂直大小,适合内容吗? [英] Qt: QPushButton vertical size, fit to contents?

查看:127
本文介绍了Qt:QPushButton 垂直大小,适合内容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法可靠地让按钮的高度适合其内容?以前的一些答案建议使用布局和大小策略,或最大高度(如何让 QPushButton 的高度自动适应文本?, 在 PyQT 中增加 QPushButton 的高度如何才能我将 QPushButton 设置为垂直增长?),但它们似乎对我不起作用.

Is there any way to reliably have a push button's height fit to its contents? Some previous answers suggest using layouts and size policies, or maximum height (How to make height of a QPushButton automatically fit the text?, Increase Height of QPushButton in PyQT, How can I set a QPushButton to grow vertically?), but they don't seem to work for me.

请参见下面的示例.这两个按钮以其默认的一行"高度绘制,因此里面的文本被剪切.将它们的大小策略设置为 PreferredMinimum 不会改变任何内容.使用 addStretch 而不是 setAlignment 不会改变任何东西(使用 none 会使按钮拉伸以填充窗口,这不是我想要的).更改最小高度是可行的,但这远不实用,除非有某种方法可以自动计算和设置它.

See the example below. The two buttons are drawn with their default "one line" height, so the text inside is clipped. Setting their size policies as Preferred or Minimum doesn't change anything. Using addStretch instead of setAlignment doesn't change anything (using none makes the buttons stretch to fill the window, which is not what I want). Changing the minimum height works, but that's far from practical, unless there's some way to automatically compute and set it.

#!/usr/bin/env python3

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QSize    


class HelloWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        button1 = QPushButton()
        l = QVBoxLayout()
        l.addWidget(QLabel('One'))
        l.addWidget(QLabel('Description of button one'))
        button1.setLayout(l)
        #button1.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        #button1.setMinimumHeight(50)

        button2 = QPushButton()
        l = QVBoxLayout()
        l.addWidget(QLabel('Two'))
        l.addWidget(QLabel('Description of button two\nwith extra line'))
        button2.setLayout(l)
        #button2.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        #button2.setMinimumHeight(65)

        layout = QVBoxLayout()
        layout.addWidget(button1)
        layout.addWidget(button2)
        #layout.addStretch(1)
        layout.setAlignment(QtCore.Qt.AlignTop)

        centralWidget = QWidget(self)          
        centralWidget.setLayout(layout)
        self.setCentralWidget(centralWidget)   

        self.resize(200, 400)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = HelloWindow()
    mainWin.show()
    sys.exit( app.exec_() )

推荐答案

问题是按钮一般没有布局,返回一个基于以下内容的大小提示:

The problem is that a push button normally has no layout, and returns a size hint based on the following:

  • 内容,如果存在的话(图标和/或文本)
  • 基于内容的当前样式(按钮斜角、焦点矩形和按钮边距)的指标,从 QStyle.sizeFromContents()

在您的情况下,最简单的解决方案是简单地为按钮文本使用换行符(例如:'One\nDescription of button one'),但如果您想添加一个不是标签的小部件(或者想要对标签本身有更多的控制),你必须覆盖 sizeHint:

In your case, the simplest solution would be to simply use the new line character for the button text (for example: 'One\nDescription of button one'), but if you want to add a widget that is not a label (or want to have more control over the label itself), you must override the sizeHint:

class ResizableButton(QtWidgets.QPushButton):
    def sizeHint(self):
        if self.layout():
            # the button now has a layout, return its size hint
            return self.layout().sizeHint()
        # no layout set, return the result of the default implementation
        return super().sizeHint()

这篇关于Qt:QPushButton 垂直大小,适合内容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆