简体   繁体   English

PyQt4可编辑组合框当前文本

[英]PyQt4 editable combobox current text

I'm using an editable QComboBox. 我正在使用可编辑的QComboBox。 What I want to achieve is to be able to type a query into the field and run that query after pressing enter. 我想要实现的是能够在字段中键入查询并在按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. 我尝试使用self.query_combo.lineEdit().text()self.query_combo.currentText()来获取文本,但在任何情况下我都无法在该字段中获取实际的当前文本值。

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. 因此,当用户按下Enter键(尖号仍在编辑字段中)时,小部件将按下该按钮并开始查询。 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. 我感觉使用QLineEdit会遇到较少的问题,但是不幸的是这不是一个选择。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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