如何在PyQt中自定义QCompleter弹出窗口? [英] How can I customize the QCompleter popup window in PyQt?
本文介绍了如何在PyQt中自定义QCompleter弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对自定义感兴趣的QLineEdit的QCompleter感兴趣.我想使它的行为类似于Chrome中的地址/搜索栏.
I have a few things for a QLineEdit's QCompleter I'm interested in customizing. I want to make it behave similar to the address / search bar in Chrome.
- 如何限制显示的行数?例如,即使有15个匹配项,我也只希望QCompleter显示5.
- 如何调整弹出窗口的大小?例如,我想使弹出窗口美观而舒适.按照上面的示例,我希望弹出窗口精确地调整为5行,而不会显示任何难看的滚动条.
- 如何移动弹出窗口?例如,我想调整弹出窗口的垂直位置,使其略低于父窗口小部件.
- 如何格式化显示的弹出窗口的列表项?例如,我要加粗匹配单词的部分.
推荐答案
- 要限制行数:将模型更改为 QStringListModel 使用 QCompleter.setModel ,并在显示弹出窗口之前设置固定数量的项目.还要确保正确设置 maxVisibleItems (默认值为7)
- 弹出窗口的大小应调整为正确的 height 自动地.弹出窗口的 width 可以通过将边距宽度(
popup.width()-popup.viewport().width()
),帧的宽度(2 * popup.frameWidth()
)和最长的字符串的宽度(popup.fontMetrics().boundingRect(string).width()
). - 可以通过传递一个弹出窗口来控制弹出窗口的位置(和宽度)适当的
QRect
QCompleter.complete . - 可以通过设置项目委托.参见此答案以富文本格式项目委托的示例为例.(但是请注意,这将影响弹出宽度的计算方式.)
- To limit the number of rows: change the model to a QStringListModel using QCompleter.setModel, and set a fixed number of items before the popup is shown. Also ensure that maxVisibleItems is set appropriately (default is seven).
- The popup window should resize to the correct height
automatically. The width of the popup can be calculated by adding together the
width of the margins (
popup.width() - popup.viewport().width()
), the width of the frame (2 * popup.frameWidth()
), and the width of the longest string (popup.fontMetrics().boundingRect(string).width()
). - The positioning (and width) of the popup can be controlled by passing an
appropriate
QRect
to QCompleter.complete. - The format of the list items could be controlled by setting an item delegate on the popup. See this answer for an example of a rich-text item delegate. (But note that this will effect how the popup width is calculated).
这篇关于如何在PyQt中自定义QCompleter弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文