問題描述
我需要一些幫助來添加一些項(xiàng)目到 QComboBox
.所以我有兩個(gè)組合框,一個(gè)根據(jù)所選項(xiàng)目填充另一個(gè).
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
用于新項(xiàng)目,它可以工作,但如果我為組合框選擇另一個(gè)選項(xiàng),它會(huì)添加新項(xiàng)目,但以前的項(xiàng)目已經(jīng)消失 - 而且有新項(xiàng)目下方的空白項(xiàng)目.
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.
我以為每次我從第一個(gè)組合框中選擇一個(gè)新選項(xiàng)來清除第二個(gè)組合框的內(nèi)容.所以我在第二個(gè)中使用了 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.
我是這么想的:
以上是當(dāng)?shù)谝粋€(gè)combobox
改變時(shí)執(zhí)行的函數(shù)的一部分.
The above is part of a function that executes when the first combobox
changes.
推薦答案
假設(shè) list1
是一個(gè)字符串列表,那么您可以簡(jiǎn)單地使用 addItems 方法:
Assuming list1
is a list of strings, then you can simply add them all at once using the addItems method:
請(qǐng)注意,您可能在示例中以錯(cuò)誤的方式使用了 QApplication.translate
.如果您想讓 list1
中的字符串可以翻譯成不同的語言,您應(yīng)該在 create 列表時(shí)這樣做,并使用 字符串字面量.
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.
例如:
另請(qǐng)注意,_fromUtf8
函數(shù)只有在您的代碼中使用包含非 ascii 字符的字符串文字時(shí)才真正有用 - 否則,它基本上是無操作的.
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.
編輯
如果您的列表包含像素圖和文本的元組,那么您可以使用以下內(nèi)容:
If your list contains, say, tuples of pixmaps and text, then you can use something like this:
這篇關(guān)于如何在 PyQt/PySide 中將項(xiàng)目添加到 QComboBox的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!