@Pompeius_Magnus
Студент

PyQt, Qt Designer — как считать индекс с выпадающего списка?

Доброго времени суток!
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())
- есть такой код.
Надо определить, что выбрано в списке, передать индекс этого элемента, и после нажатия кнопки выполнить код.
Как связать функцию, и кнопку - я разобрался. А как получить индекс выбранного элемента выпадающего списка, нет.
Есть какая-нибудь функция getIndex? и где можно посмотреть примеры? www.riverbankcomputing.com/software/pyqt/intro - искал тут, но не нашел.
Спасибо за помощь!
  • Вопрос задан
  • 3517 просмотров
Решения вопроса 1
@jkrieger
Предыдуший мой комментарий был неудачно оформлен.
Немного изменил код, теперь должно работать.

#-*- 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_())
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
@vilgeforce
Раздолбай и программист
QComboBox::curreintIndex - не оно?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
11 мая 2024, в 00:19
1000 руб./за проект
10 мая 2024, в 23:51
30000 руб./за проект
10 мая 2024, в 23:33
2500 руб./за проект