我碰巧使用了 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.
set()
(在示例中)的 ttk.Combobox
方法等效的是 placeholderText
的 QComboBox
属性:
self.comboBox.setPlaceholderText("--Choose One--")
self.ui.combobox.clearEditText()
self.ui.combobox.setPlaceholderText("--choose-one--")
self.ui.combobox.setCurrentIndex(-1)