A
A
Alexander Krasnov2015-04-09 13:50:43
Python
Alexander Krasnov, 2015-04-09 13:50:43

PyQt, Qt Designer - how to read index from drop down list?

Good day!

from PyQt4 import QtCore, QtGui
import sys
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(QtGui.QWidget):
  def __init__(self):
    QtGui.QWidget.__init__(self)
    self.setupUi(self)
  def setupUi(self, Form):
    Form.setObjectName(_fromUtf8("Form"))
    Form.resize(200, 300)
    self.comboBox = QtGui.QComboBox(Form)
    self.comboBox.setGeometry(QtCore.QRect(20, 20, 61, 21))
    self.comboBox.setObjectName(_fromUtf8("comboBox"))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.comboBox.addItem(_fromUtf8(""))
    self.pushButton = QtGui.QPushButton(Form)
    self.pushButton.setGeometry(QtCore.QRect(20, 50, 100, 50))
    self.pushButton.setObjectName(_fromUtf8("pushButton"))

    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)
    #QtCore.QObject.connect(self.comboBox, QtCore.SIGNAL(_fromUtf8("currentIndexChanged(int)")), self.pushButton.click)

  def retranslateUi(self, Form):
    index;
    Form.setWindowTitle(_translate("Form", "Form", None))
    self.comboBox.setItemText(3, _translate("Form", ".60", None))
    self.comboBox.setItemText(2, _translate("Form", ".45", None))
    self.comboBox.setItemText(1, _translate("Form", ".19", None))
    self.pushButton.setText(_translate("Form", "SEND!", None))
    #self.comboBox.changes(self.setIndex)
    self.pushButton.clicked.connect(self.sendIndexFromComboBox)
  def sendIndexFromComboBox(self,index):
    if index == 0: print ("Send to .45")
    elif index == 1: print ("Send to .19")
    elif index == 2: print ("Send to .60")
    elif index == 3: print ("Send other stand")
if __name__ == '__main__':
  app = QtGui.QApplication(sys.argv)
  ex = Ui_Form()
  ex.show()
  sys.exit(app.exec())
- there is such a code.
It is necessary to determine what is selected in the list, pass the index of this element, and after pressing the button, execute the code.
How to connect a function and a button - I figured it out. And how to get the index of the selected item in the dropdown list, no.
Is there any getIndex function? where can i see examples? www.riverbankcomputing.com/software/pyqt/intro - searched here but didn't find it.
Thanks for the help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jkrieger, 2015-04-09
@Pompeius_Magnus

My previous comment was badly framed.
Changed the code a bit, it should work now.

#-*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(QtGui.QWidget):
  def __init__(self):
    QtGui.QWidget.__init__(self)
    self.setupUi(self)

  def setupUi(self, Form):
    Form.setObjectName(_fromUtf8("Form"))
    Form.resize(200, 300)
    self.comboBox = QtGui.QComboBox(Form)
    self.comboBox.setGeometry(QtCore.QRect(20, 20, 61, 21))
    self.comboBox.setObjectName(_fromUtf8("comboBox"))
#    self.comboBox.addItem(_fromUtf8(""))
#    self.comboBox.addItem(_fromUtf8(""))
#    self.comboBox.addItem(_fromUtf8(""))

    self.comboBox.addItem(_translate("Form", ".60", None))
    self.comboBox.addItem(_translate("Form", ".45", None))
    self.comboBox.addItem(_translate("Form", ".19", None))

    self.pushButton = QtGui.QPushButton(Form)
    self.pushButton.setGeometry(QtCore.QRect(20, 50, 100, 50))
    self.pushButton.setObjectName(_fromUtf8("pushButton"))

    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)
    #QtCore.QObject.connect(self.comboBox, QtCore.SIGNAL(_fromUtf8("currentIndexChanged(int)")), self.pushButton.click)

  def retranslateUi(self, Form):
    #index;
    Form.setWindowTitle(_translate("Form", "Form", None))

    self.pushButton.setText(_translate("Form", "SEND!", None))
    self.pushButton.clicked.connect(self.sendIndexFromComboBox)

  def sendIndexFromComboBox(self,index):
  
    index = self.comboBox.currentIndex()
    print index

    text = self.comboBox.currentText()
    print text

#    if index == 0: print ("Send to .45")
#    elif index == 1: print ("Send to .19")
#    elif index == 2: print ("Send to .60")
#    elif index == 3: print ("Send other stand")


if __name__ == '__main__':
  app = QtGui.QApplication(sys.argv)
  ex = Ui_Form()
  ex.show()
  sys.exit(app.exec_())

V
Vladimir Martyanov, 2015-04-09
@vilgeforce

QComboBox::curreintIndex - isn't it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question