使用 QT Designer 时,如何使 PyQt QLineEdit 小部件比默认大小 17 x 窄?
当我使用 QT Designer 将 Qt 小部件放入 QHBoxLayout
布局中时,QLineEdit
小部件的最小宽度似乎为 17 x,至少如果这是当前源代码的话:
https://github.com/qt/qtbase/blob/dev/src/widgets/widgets/qlineedit.cpp
我找不到一种方法让 PyQt5 放置这些小部件,这样它们就会比这是默认值,但如果字体更改,大小仍然会更改。
例如,QComboBox
将自动布局,使其宽度足以显示作为可能值输入的最长文本。如果在 Designer 中为组合框输入的最长文本为 5 个字符,则与最小 QLineEdit
宽度相比,组合框的布局将相当窄。如何使用 QT Designer 配置 QLineEdit
,使其始终足够宽以容纳那么多字符,无论字体设置为什么,并且不更宽?我知道在设计器中我可以输入 maxLength
,这将限制可以输入/显示的最大字符数,但该设置当然对布局没有影响。
例如,我有一些文本框,其中的字符永远不会超过 5 个,而 Designer 的布局使它们至少比我需要的宽 3 倍。这是使用默认的“扩展”水平策略,但我尝试了水平策略和最小尺寸或基本尺寸值的多种组合。我希望允许人们使用不同的字体大小,因此我无法安全地设置最大像素大小,也无法安全地设置固定的水平大小。对 QComboBox 的处理正是我想要的 QLineEdit 的处理。
这一切都是在 python 中使用 Pip 上可用的最新版本的 PyQt5、pyqt5 5.15.6 和 pyqt5-qt5 5.15.2 实现的。
When I put Qt widgets in a QHBoxLayout
layout using QT Designer, there seems to be a minimum width for QLineEdit
widgets of 17 x's, at least if this is the current source code:
https://github.com/qt/qtbase/blob/dev/src/widgets/widgets/qlineedit.cpp
I cannot find a way to make PyQt5 lay those widgets out so they will be narrower than this default, but still change size if the font is changed.
As an example, a QComboBox
will be automatically laid out so that it is just wide enough to display the longest text that is entered as a possible value. If the longest text entered for a combobox in Designer is 5 characters, the combobox will be laid out as quite narrow compared to the minimum QLineEdit
width. How can I configure a QLineEdit
with a number of characters so that it is always wide enough for that many characters, whatever the font is set to, and no wider, using just QT Designer? I know in Designer I can enter maxLength
, which will limit the maximum number of characters that can be entered/displayed, but that setting of course has no effect on the layout.
I have some text boxes that will never have more than 5 characters in them, for example, and the layout with Designer makes them at least 3 times wider than I will ever need. This is using the default "Expanding" horizontal policy, but I have tried many combinations of horizontal policy and values for minimum size or base size. I want to allow for people to have different font sizes, so I cannot safely set a maximum pixel size and I cannot safely set a fixed horizontal size. The handling for QComboBox
es is precisely what I want for QLineEdit
s.
This is all in python using the latest versions of PyQt5 available on Pip, pyqt5 5.15.6 and pyqt5-qt5 5.15.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QLineEdit 的大小提示是考虑到各个方面来计算的,其中大多数使用未公开给 API 的私有函数,并且“x”计数是硬编码的,这意味着这不能直接从 Designer 实现,只能通过子类化。
虽然我们可以尝试模仿其行为来实现自定义字符大小,但我认为对于简单的情况来说这是不必要的,因此我通过采用默认大小提示并根据差异调整宽度来简化概念介于自定义字符提示和默认的 17 个“x”计数之间。
使用上面的代码,您可以通过添加标准 QLineEdit 并使用类名和相对 python 文件(不带文件扩展名)作为标题来提升它,从而在 Designer 中使用自定义小部件。您还可以在 Designer 中将
charHint
设置为动态属性,加载 UI 时将为小部件正确设置它。The size hint of QLineEdit is computed considering various aspects, most of them using private functions that are not exposed to the API, and the "x" count is hardcoded, meaning that this cannot be achieved directly from Designer, and can only be done through subclassing.
While we could try to mimic its behavior to implement a custom character size, I believe it is unnecessary for simple cases, so I simplified the concept by taking the default size hint and adapting the width based on the difference between the custom character hint and the default 17 "x" count.
With the above code, you can use the custom widget in Designer by adding a standard QLineEdit and promoting it with the class name and relative python file (without the file extension) as header. You can also set the
charHint
as a dynamic property in Designer, and it will be properly set for the widget when the UI is loaded.