PyQt QComboBox 设置下拉菜单中可见项目的数量 [英] PyQt QComboBox setting number of visible items in dropdown

查看:153
本文介绍了PyQt QComboBox 设置下拉菜单中可见项目的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 PyQt 中开发一个应用程序,该应用程序采用对象字典,并允许您实时绘制来自机器人的变量.我正在努力启用此功能的一件事是下拉菜单.不幸的是,我们有几百个变量,所以我的 PyQt Combobox 会在点击时从屏幕顶部弹出到底部带有项目.我想将一次显示的项目数量限制为 20,并能够滚动查看其余项目.我已经尝试使用记录在案的 setMaxVisibleItems 方法,但它根本不影响下拉列表.有什么建议吗?

I'm working on an application in PyQt that takes an object dictionary and allows you to plot the variables streaming from a robot in real time. One of the things I'm working on to enable this is a drop down menu. Unfortunately, we have a couple hundred variables, so my PyQt Combobox pops up from the top of the screen to the bottom with items when clicked. I'd like to limit the number of items displayed at a time to 20, with the ability to scroll to see the rest. I've tried using the documented setMaxVisibleItems method, but it doesn't affect the drop down at all. Any recommendations?

代码在这里:

#!/usr/bin/env python

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QComboBox, QApplication

from cli.parc2_od import cli_od
import sys

app = QApplication(sys.argv)

items = cli_od.OD.keys() #Items to populate dropdown.
combo = QComboBox()

#The solution:
combo.setStyleSheet("QComboBox { combobox-popup: 0; }")
combo.setMaxVisibleItems(10)




combo.addItems(items)

combo.resize(300, 30)
combo.show()


sys.exit(app.exec_())

推荐答案

根据文档:

对于在 `QStyle::SH_ComboBox_Popup 如 Mac 风格或 Gtk+ 风格.

The property maxVisibleItems is ignored for non-editable comboboxes in styles that returns true for `QStyle::SH_ComboBox_Popup such as the Mac style or the Gtk+ Style.

并且您可以使用样式表覆盖 SH_ComboBox_Popup 样式提示:

And you can override that SH_ComboBox_Popup style hint with a style sheet:

combo.setStyleSheet("QComboBox { combobox-popup: 0; }");

这篇关于PyQt QComboBox 设置下拉菜单中可见项目的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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