gpt4 book ai didi

qt - 将 QPushButton 宽度缩小到最小

转载 作者:行者123 更新时间:2023-12-02 05:56:30 27 4
gpt4 key购买 nike

这似乎是一个很简单的事情,但我似乎无法弄清楚。如何使按钮的宽度最小。它不断扩展到我放入的布局的宽度。在下面的示例中,QPushButton 宽度最终与 QLabel 相同:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class MyWindow(QWidget):

def __init__(self,parent = None):

QWidget.__init__(self,parent)

layout = QVBoxLayout()
layout.addWidget(QLabel('this is a really, really long label that goes on and on'))
layout.addWidget(QPushButton('short button'))

self.setLayout(layout)

if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())

最佳答案

setMaximumWidth 对我有用

from PyQt4 import QtGui

class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
layout = QtGui.QHBoxLayout()
texts = [":)",
"&Short",
"&Longer",
"&Different && text",
"More && text",
"Even longer button text", ]
for text in texts:
btn = QtGui.QPushButton(text)
double = text.count('&&')
text = text.replace('&', '') + ('&' * double)
width = btn.fontMetrics().boundingRect(text).width() + 7
btn.setMaximumWidth(width)
layout.addWidget(btn)
self.setLayout(layout)

if __name__ == '__main__':
import sys

app = QtGui.QApplication(sys.argv)
mainWin = Window()
mainWin.show()
sys.exit(app.exec_())

关于qt - 将 QPushButton 宽度缩小到最小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14418375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com