Fill in a QComboBox with a list

I am developing a GUI dialog using PyQT4, which imports some data into a Pandas DataFrame, and then outputs the data to the Matplotlib built-in canvas.

I would like to pass a list of variables from a DataFrame to a combo box. My first attempt:

list = list(df.parameter,unique())
self.FirstComboBox = QtGui.QComboBox()
self.FirstComboBox.addItems(list)

But when I start, I get

TypeError: QComboBox.addItems(QStringList): argument 1 has unexpected type 'list'

I have seen examples where a sorted list of dict keys is passed to a combo box, so I am confused by the fact that I can not pass the list.

Ben

+4
source share
2 answers

It looks like you are using the old v1 api. You can use the new api , which eliminates the need for casting strings in QStrings(or QStringListsin this case).

import sip
# Do this before you import PyQt
sip.setapi('QString', 2)

from PyQt4 import QtCore
+1

, , . .

        for i in range(len(channels)):
            self.MyComboBox.addItem(channels[i])
+1

All Articles