Advertisement
furas

Python - PyQt5 - ComboBox

Oct 2nd, 2016
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. #
  2. # http://stackoverflow.com/questions/39814304/python-3-5-pyqt5-index-of-comboboxes-and-their-choosen-options
  3. #
  4.  
  5. from PyQt5 import QtCore
  6. from PyQt5 import QtWidgets
  7. import sys
  8.  
  9. app = QtWidgets.QApplication(sys.argv)
  10.  
  11. ########################## Create table ##############################
  12.  
  13. tableWidget = QtWidgets.QTableWidget()
  14. tableWidget.setGeometry(QtCore.QRect(200, 200, 250, 300))
  15. tableWidget.setColumnCount(2)
  16. tableWidget.setRowCount(9)
  17. tableWidget.show()
  18.  
  19. ################# Create tablecontent & comboboxes ###################
  20.  
  21. def dosomething(index):
  22.     widget = app.sender()
  23.     print('widget object:', widget)
  24.     print('widget myname:', widget.my_name)
  25.     print('widget index:', combo_list.index(widget))
  26.     print('option index:', index)
  27.  
  28. #---------------------------------------------------------------------
  29.  
  30. names = ['Name 1', 'Name 2', 'Name 3', 'Name 4']
  31. combo_option_list = ["Choose...", "Option 1", "Option 2", "Option 3", "Option 4"]
  32.  
  33. combo_list = []
  34.  
  35. for i, name in enumerate(names):
  36.  
  37.     tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(name))
  38.  
  39.     combobox = QtWidgets.QComboBox()
  40.     combobox.addItems(combo_option_list)
  41.     combobox.my_name = name + ' (i=' + str(i) + ')'
  42.     combobox.currentIndexChanged.connect(dosomething)
  43.  
  44.     combo_list.append(combobox)
  45.  
  46.     tableWidget.setCellWidget(i, 1, combobox)
  47.  
  48. #---------------------------------------------------------------------
  49.  
  50. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement