简体   繁体   中英

PyQt4 editable combobox current text

I'm using an editable QComboBox. What I want to achieve is to be able to type a query into the field and run that query after pressing enter. But not matter how I try to get the typed query, combobox's value is always the first item in the list, not the string I just typed. I tried getting the text by using self.query_combo.lineEdit().text() and self.query_combo.currentText() , but in no case am I able to get the actual current text value in the field.

Does anyone know why this is so and how to solve it?

self.query_combo = QComboBox(query_box)
self.query_combo.setEditable(True)
query_box.layout().addWidget(self.query_combo)
# combo box is filled with some example items

The user then proceeds to type in the input text. While typing, the "start query" button is highlighted. Therefore when the user presses the enter key (while the caret is still in the editing field), the widget is going to push that button and start the query. In the query method, the code is as follows:

def run_initial_query(self):
    # Query keywords.
    qkw = self.query_combo.currentText()
    # or
    qkw = self.query_combo.lineEdit().text()

but neither produces the newly typed value. The combo box always selects the first example list item, that was already in the list. I have a feeling I would have less problems using a QLineEdit, but unfortunately that isn't an option.

I managed to do what you want in this way

def __init__(self):
    ...
    self.ui.comboBox.currentIndexChanged.connect(self.run_initial_query) 

def run_initial_query(self):
    #Do stuff
    print self.ui.comboBox.currentText()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM