PyQt5 ComboBox 虚拟占位符

问题描述 投票:0回答:2

我碰巧使用了 qcombobox,但找不到添加虚拟占位符的方法(就像我们在 tkinter 中那样)。 例如,我想要的组合框是这样的:

--选择一个--

苹果

香蕉

猕猴桃

我已经具备了要素;苹果、香蕉、猕猴桃已就位,但我希望当我点击它时,“选择一个”消失。

**Tkinter version**

from tkinter import ttk
#---
#some code here
#---
lst = ['Apple','Banana','Kiwi']

self.w0 = ttk.Combobox(self, values = lst, state='readonly')
self.w0.set('--Choose One--') ### This is the functionality that I need
self.w0.pack()

**PyQt version**
from PyQt5 import QtWidgets
lst = ['Apple','Banana','Kiwi']
#---
#some code here
#---
self.comboBox = QtWidgets.QComboBox()
self.comboBox.addItems(lst)
self.layout.addWidget(self.comboBox) ## self.layout is defined but not shown here

### I might add it as an item of the comboBox like
### self.comboBox.addItem('--Choose One--') but it then becomes selectable 
### which I don't want it to.
python pyqt5 qcombobox
2个回答
1
投票

set()
(在示例中)的
ttk.Combobox
方法等效的是
placeholderText
QComboBox
属性:

self.comboBox.setPlaceholderText("--Choose One--")

0
投票
self.ui.combobox.clearEditText()
self.ui.combobox.setPlaceholderText("--choose-one--")
self.ui.combobox.setCurrentIndex(-1)
© www.soinside.com 2019 - 2024. All rights reserved.