0

I happen to have used the qcombobox, but could not find a way to add a dummy placeholder (like we have in tkinter). For example, the combobox I'd like to have is something like this:

--Choose One--

Apple

Banana

Kiwi

I already have the elements; Apple, Banana, Kiwi in place but I want the --Choose One-- vanished when I click on it.

**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.
SS93
  • 1
  • 2
  • 1
    You point out that this functionality is implemented in tkinter, and as I see it it is not, you could place the code that implements that logic with tkinter to take it as a basis since the schema you provide helps to understand how it looks but does not indicate how it behaves. – eyllanesc Sep 24 '20 at 05:32
  • Yes you are right, but the code does not consist of just that part of course, but it requires the functionality. I just wanted to know is there any way to achieve it, I just did not think I had to share the rest of the code – SS93 Sep 24 '20 at 05:47
  • 1
    mmm, you point out: *like we have in tkinter*, so my request is that you provide that code (the one that involves tkinter) so as to take it as inspiration since I am still not clear about your objective. – eyllanesc Sep 24 '20 at 05:50
  • Well, I added some lines, hope it helps to clear up the problem. Thanks! – SS93 Sep 24 '20 at 06:19

1 Answers1

1

The equivalent to the set() method of the ttk.Combobox (in the example) is the placeholderText property of QComboBox:

self.comboBox.setPlaceholderText("--Choose One--")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • It returns QComboBox object has no attribute error. I've also tried solution given here: https://stackoverflow.com/questions/18274508/setplaceholdertext-for-qcombobox – SS93 Sep 24 '20 at 07:42
  • @SS93 mmm, what version of pyqt5 do you use? that method exists for version 5.15 or higher – eyllanesc Sep 24 '20 at 11:54