5

I'm developing a GUI dialog using PyQT4 which imports some data into a Pandas DataFrame and then plots the data to an embedded Matplotlib canvas.

I'd like to pass a list of variable from the DataFrame to the combo box. My first attempt was:

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

But on running this I get

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

I've seen examples where a sorted list of dict keys is passed to a combo box, so I'm confused that I can't pass a list.

Ben

Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
BMichell
  • 3,581
  • 5
  • 23
  • 31

3 Answers3

3

In the end I got this to work. But I'm not happy with it.

        for i in range(len(channels)):
            self.MyComboBox.addItem(channels[i])
BMichell
  • 3,581
  • 5
  • 23
  • 31
  • In Python, this sort of explicit iteration trivially reduces to: `for channel in channels: self.MyComboBox.addItem(channel)`. PySide2 sadly appears to require similar circumvention. In particular, the `"TypeError: 'PySide2.QtWidgets.QComboBox.addItems' called with wrong argument types"` exception is raised on attempting to pass a pure-Python sequence of strings to *any* `addItems()` method – a blatant failure of the underlying binding generator (e.g., SIP, Shiboken2). `` – Cecil Curry Sep 25 '17 at 07:20
  • 1
    **Oh, I see.** In my case, I was attempting to pass a view of `OrderedDict` values (i.e., the sequence returned by the `OrderedDict.values()` method) to the `QComboBox.addItems()` method. Since this view *is* a valid sequence, this should technically work. It doesn't, because the binding generator appears to only support `tuple` and `list` sequences. In my case, the solution was thus to coerce the desired sequence into a `tuple` first: e.g., `self.MyComboBox.addItems(tuple(channels))`. – Cecil Curry Sep 25 '17 at 07:34
2

It looks like you're using the old v1 api. You can use the newer api, which removes the need for casting strings to QStrings (or QStringLists in this case).

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

from PyQt4 import QtCore
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
0

The addItems works for this

months = ['Jan', 'Feb', 'Mar']
cbo = QComboBox()
cbo.addItems(months)
QuentinJS
  • 162
  • 1
  • 9