How to append Item text in EditText in QComboBox
-
I'm programming a Combobox widget for a Tag system.
I subclassed aQComboBox
asTabComboBox
. It has an editable LineEdit, i already set it. As shown in pic, it should have two features:
1.popup still shown unless the area out of popup clicked (Solved)
2.append item text behind current LineEdit text when item clicked
I tried to build a variable inTabComboBox
to store current LineEdit text, and connectcurrentIndexChanged
signal to my custom function_add_tag
. But when I click item, if item text isToby
, current LineEdit text will change toToby,Toby
, it should beBob,Toby
.
Here is my code, contains popup holder.class TagComboBox(QComboBox): def __init__(self, tag_list, parent=None): super(TagComboBox, self).__init__(parent) self.setEditable(True) self.setFrame(False) self.addItems(tag_list) self.currentIndexChanged.connect(self._add_tag) def hidePopup(self): popup_view = self.view() if popup_view.rect().contains(popup_view.mapFromGlobal(QCursor().pos()))==False: super().hidePopup() def _add_tag(self, index): tag_str = self.itemText(index) src_str = self.lineEdit().text() dist_str = src_str + ',' + tag_str self.lineEdit().setText(dist_str)
-
@crisp_toby said in How to append Item text in EditText in QComboBox:
I tried to build a variable in TabComboBox to store current LineEdit text, and connect currentIndexChanged signal to my custom function_add_tag . But when I click item, if item text is Toby, current LineEdit text will change to Toby,Toby, it should be Bob,Toby.
You want
Bob, Toby
but you getToby, Toby
, correct?!You know what
currentIndexChanged
does? It is emitted after the text changed... so you already get the new, updated index and text from it...
In your function you store the current text and text at index in a string and display it...
However at the time your function is called both are identical as the lineEdit was updated and the notifier signal was fired, which you catch and in turn call your function.You need to store/update this
src_str = self.lineEdit().text()
somewhere else and not inside the signal-connected function for your current "iteration". You can save it there for the next one.
Something like:
def _add_tag(self, index): tag_str = self.itemText(index) dist_str = self.old_str + ',' + tag_str ## this is bad as in C++ the lineEdit is const, might work in Python though self.lineEdit().setText(dist_str) ## update former selection for next text update self.old_str = self.itemText(index)
-
This post is deleted!
-
@Pl45m4 It works! But the last line
self.old_str = self.itemText(index)
, it could be a mistake,self.old_str
should updated byself.lineEdit().text()
.
I test the code, found some new problem.
As I modified, I useactivated
signal to connect_add_tag
, andset_value
function for outer delegate initiate data at the beginning,old_str
updated in this function.
As shown in picture, if I click one Item, item text append in LineEdit, then I delete that item text, and click other Item, deleted Item text appeared again.
It should beold_str
not be updated when edit LineText, but when I connect theeditTextChanged
signal to_update_old_str
, Former problem likeToby,Toby
happened again.
here is the modified code.class TagComboBox(QComboBox): def __init__(self, tag_list, parent=None): super(TagComboBox, self).__init__(parent) self.setEditable(True) self.setFrame(False) self.addItems(tag_list) self.old_str = '' self.activated.connect(self._add_tag) self.editTextChanged.connect(self._update_tag) def hidePopup(self): popup_view = self.view() if popup_view.rect().contains(popup_view.mapFromGlobal(QCursor().pos()))==False: super().hidePopup() def _add_tag(self, index): tag_str = self.itemText(index) dist_str = self.old_str + ',' + tag_str self.lineEdit().setText(dist_str) self.old_str = self.lineEdit().text() def _update_old_str(self): self.old_str = self.lineEdit().text() def get_value(self): return self.lineEdit().text() def set_value(self, value): self.lineEdit().setText(value) self.old_str = value
-
I wasn't sure if you want to show only the last and the new selection as a pair or if you want to append every new selected item continuously.
@crisp_toby said in How to append Item text in EditText in QComboBox:
It should be old_str not be updated when edit LineText, but when I connect the editTextChanged signal to _update_old_str, Former problem like Toby,Toby happened again.
Really think about what you are doing and what is happening.
The problem is easy to solve if you know when which string is updated to which value. -