Python PyQt:如何在知道项目文本(标题)的情况下将组合框设置为项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22797794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:42:46  来源:igfitidea点击:

PyQt: How to set Combobox to Item knowing Item's text (a title)

pythonpyqtpyqt5pyqt4qcombobox

提问by alphanumeric

Is it possible to set Combobox to an item knowing an Item's text value. I am trying to avoid looping through for i in range(myCombobox.count())just to find an Item's index so it could be used to set a combobox to that item's index.

是否可以将 Combobox 设置为知道项目文本值的项目。我试图避免循环遍历for i in range(myCombobox.count())只是为了找到一个项目的索引,所以它可以用于将组合框设置为该项目的索引。

采纳答案by ekhumoro

Yes, there is QComboBox.findText, which will return the index of the matched item (or -1, if there isn't one). By default, the search does exact, case-sensitive matching, but you can tweak the behaviour by passing some match-flagsas the second argument. For example, to do case-insensitivematching:

是的,有QComboBox.findText,它将返回匹配项的索引(或者-1,如果没有)。默认情况下,搜索进行精确的、区分大小写的匹配,但您可以通过传递一些匹配标志作为第二个参数来调整行为。例如,要进行不区分大小写的匹配:

    index = combo.findText(text, QtCore.Qt.MatchFixedString)
    if index >= 0:
         combo.setCurrentIndex(index)

There is also an equivalent findDatamethod that matches by the item's data.

还有一个等效的findData方法,它与项目的数据相匹配。