問題描述
我需要一些幫助來添加一些項目到 QComboBox
.所以我有兩個組合框,一個根據所選項目填充另一個.
I need some help adding some items to a QComboBox
. So I have two comboboxes, and one populates the other depending on the item selected.
我的問題是,將 additem
用于新項目,它可以工作,但如果我為組合框選擇另一個選項,它會添加新項目,但以前的項目已經消失 - 而且有新項目下方的空白項目.
My question is that, using additem
for new items, it works, but if I choose another option for the combobox, it adds the new items, but the previous items are gone - and there are blank items below the new ones.
我以為每次我從第一個組合框中選擇一個新選項來清除第二個組合框的內容.所以我在第二個中使用了 clear()
- 但它不起作用.
I thought that each time I chose a new option from the first combobox to clear the contents of the second combobox. So I used the clear()
on the second - but it didn't work.
我是這么想的:
以上是當第一個combobox
改變時執行的函數的一部分.
The above is part of a function that executes when the first combobox
changes.
推薦答案
假設 list1
是一個字符串列表,那么您可以簡單地使用 addItems 方法:
Assuming list1
is a list of strings, then you can simply add them all at once using the addItems method:
請注意,您可能在示例中以錯誤的方式使用了 QApplication.translate
.如果您想讓 list1
中的字符串可以翻譯成不同的語言,您應該在 create 列表時這樣做,并使用 字符串字面量.
Note that you are probably using QApplication.translate
in the wrong way in your example. If you want to make it possible for the strings in list1
to be translated into a different language, you should do that when you create the the list, and use string literals.
例如:
另請注意,_fromUtf8
函數只有在您的代碼中使用包含非 ascii 字符的字符串文字時才真正有用 - 否則,它基本上是無操作的.
Also note that the _fromUtf8
function is only really useful if you're using string literals containing non-ascii characters in your code - otherwise, it's basically a no-op.
編輯
如果您的列表包含像素圖和文本的元組,那么您可以使用以下內容:
If your list contains, say, tuples of pixmaps and text, then you can use something like this:
這篇關于如何在 PyQt/PySide 中將項目添加到 QComboBox的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!