PySide: Connecting QComboBox indices with strings



  • I would like to connect the QComboBox indeces with specific strings (i.e. when I select "A", I want that it prints "A has been selected", and when I select "B", then "B has been selected").

    I am new to PySide and learning, so I am sure there exists a simple solution. Help is appreciated.

    @from PySide import QtGui

    class Widget(QtGui.QWidget):
    
        def __init__(self, parent=None):
            super().__init__(parent)
    
            v_global_layout = QtGui.QVBoxLayout()
    
            method_selection = QtGui.QComboBox()
            method_selection.addItem("A")
            method_selection.addItem("B")
    
            v_global_layout.addWidget(method_selection)
            self.setLayout(v_global_layout)
    
            def do_somethinh():
                print("A has been selected!!!")
            method_selection.activated.connect(do_somethinh)
    
    
    if __name__ == '__main__':
        import sys
        app = QtGui.QApplication(sys.argv)
    
        main_window = Widget()
        main_window.setGeometry(100, 100, 640, 480)
        main_window.show()
    
        sys.exit(app.exec_())
    

    @


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    AFAIK (haven't used PyQt/PySide for while)

    @
    def do_something(self, index):
    print "{} has been selected!!!".format(self.itemText(index))
    @

    should to it

    Hope it helps



  • Thsnk you!

    I also found another way to do it:

    @def do_somethinh(text):
    print(text, "has been selected!!!")
    method_selection.activated[str].connect(do_somethinh)@


  • Lifetime Qt Champion

    You're welcome !

    Yes, that's also a good solution

    Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.