Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 365
user985366 Avatar asked Aug 10 '12 12:08

user985366


2 Answers

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...

like image 75
Oleh Prypin Avatar answered Sep 22 '22 22:09

Oleh Prypin


Thanks Oleh Prypin! Your answer helped me when I came across the obscure arg__1 in the PySide documentation.

When I tested both combo.currentIndexChanged[str] and combo.currentIndexChanged[unicode], each signal sent the unicode version of the current index text.

Here's an example that demonstrates the behavior:

from PySide import QtCore
from PySide import QtGui

class myDialog(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        super(myDialog, self).__init__(*args, **kwargs)

        combo = QtGui.QComboBox()
        combo.addItem('Dog', 'Dog')
        combo.addItem('Cat', 'Cat')

        layout = QtGui.QVBoxLayout()
        layout.addWidget(combo)

        self.setLayout(layout)

        combo.currentIndexChanged[int].connect(self.intChanged)
        combo.currentIndexChanged[str].connect(self.strChanged)
        combo.currentIndexChanged[unicode].connect(self.unicodeChanged)

        combo.setCurrentIndex(1)

    def intChanged(self, index):
        print "Combo Index: "
        print index
        print type(index)

    def strChanged(self, value):
        print "Combo String:"
        print type(value)
        print value

    def unicodeChanged(self, value):
        print "Combo Unicode String:"
        print type(value)
        print value

if __name__ == "__main__":

    app = QtGui.QApplication([])
    dialog = myDialog()
    dialog.show()
    app.exec_()

The resulting output is:

Combo Index
1
<type 'int'>
Combo String
<type 'unicode'>
Cat
Combo Unicode String
<type 'unicode'>
Cat

I also confirmed that basestring will throw an error IndexError: Signature currentIndexChanged(PyObject) not found for signal: currentIndexChanged. PySide appears to differentiate int, float (which it refers to as double), str/unicode (which both become unicode), and bool, but all other python types are parsed as PyObject for the purpose of signal signatures.

Hope that helps someone!

like image 2
flutefreak7 Avatar answered Sep 19 '22 22:09

flutefreak7