Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PySide: Connecting QComboBox indices with strings
Qt 6.11 is out! See what's new in the release blog

PySide: Connecting QComboBox indices with strings

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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_())
    

    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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)@

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved