Search code examples
signalsoverloadingpysideqcombobox

Overloaded pyside signals (QComboBox)


Using a QComboBox with pyside, I know how to connect the signal and use the index that it sends. But what about the unicode argument? If I'd prefer to connect to something that wants the string from the combobox, is that possible?

From: http://www.pyside.org/docs/pyside/PySide/QtGui/QComboBox.html#PySide.QtGui.QComboBox

All three signals exist in two versions, one with a PySide.QtCore.QString argument and one with an int argument.

Signals

def activated (arg__1)
def activated (index)

PySide.QtGui.QComboBox.activated(index) Parameters: index – PySide.QtCore.int

PySide.QtGui.QComboBox.activated(arg_1) Parameters: arg_1 – unicode

Edit: some code.

le = ComboBoxIpPrefix()
le.currentIndexChanged.connect(lambda x....)

This code gives me the index. The question was how to get the unicode string mentioned in the docs.


Solution

  • I don't understand what exactly your question is.

    There are two versions of QComboBox.activated signal. One gives you the index of the selected item, the other one gives you its text.

    To choose between them in PySide you do the following:

    a_combo_box.activated[int].connect(some_callable)
    
    a_combo_box.activated[str].connect(other_callable)
    

    The second line probably won't work this way in Python 2, so substitute str with unicode.

    Note that I use general (C++) Qt documentation, because PySide documentation is still quite ambigous: I kept seeing those arg__1s everywhere...
    "Translating" to Python shouldn't be too hard. Just keep in mind that QString becomes str (or unicode in Python 2; by the way, I like having my code work on all versions of Python, so I usually make an alias type text that is str in Py3 and unicode in Py2); long, short, etc. become int; double becomes float; QVariant is completely avoided, it just means that any data type can be passed there; and so on...