Grab qcomboxbox index

12 views
Skip to first unread message

likage

unread,
Oct 10, 2014, 6:02:03 AM10/10/14
to python_in...@googlegroups.com
I have a QComboBox in which it contains 2 items.

How can I code it in such a way that when the first item in the combobox is selected, it will call out the function that is for the first item. Likewise, if the second item is selected, it will call out the function for the second item, something similar like using list index [0], [1] etc..

self.methodCombo = QComboBox()
self.methodCombo.addItem('method01')
self.methodCombo.addItem('method02')




Marcus Ottosson

unread,
Oct 10, 2014, 6:12:14 AM10/10/14
to python_in...@googlegroups.com
Have look at the currentIndexChanged signal.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/6c1e4e7e-74e4-4976-b7a5-f6cfa252016a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Oct 10, 2014, 6:22:24 AM10/10/14
to python_in...@googlegroups.com
There are a couple ways to do this. One is to store your callbacks as the data for each item:

class Test(QtGui.QDialog):

    def __init__(self):
        super(Test, self).__init__()

        self.combo = QtGui.QComboBox()
        self.combo.addItem("First Callback", self.callback1)
        self.combo.addItem("Second Callback", self.callback2)

        self.combo.activated.connect(self._comboActivated)

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

    def callback1(self):
        print "callback1"

    def callback2(self):
        print "callback2"

    def _comboActivated(self, idx):
        fn = self.combo.itemData(idx)
        fn()


Fredrik Averpil

unread,
Oct 20, 2014, 2:56:32 AM10/20/14
to python_in...@googlegroups.com
That's pretty neat. I would have used a whole lot more code. Thanks for the tip, Justin.

Justin Israel

unread,
Oct 20, 2014, 3:19:40 AM10/20/14
to python_in...@googlegroups.com

On 20/10/2014 7:56 PM, "Fredrik Averpil" <fredrik...@gmail.com> wrote:
>
> That's pretty neat. I would have used a whole lot more code. Thanks for the tip, Justin.
>

No problem!

> To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAD%3DwhWNfD_LxcBLDn-nhK6BrMSCOogTxBZCOckFCw7MXxnxA4Q%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages