0

I have a new QLineEdit(box) for the subject code.Each subject has a subject code(for eg Sentiment Analysis has code CS01,Data Cleansing has CS02,etc.).When i select one subject(Sentiment Analysis) its subject code(CS01) should be displayed in the new edit box and when i choose another subject(Data CLeansing) its subject code(CS02) should be dispalyed .How should the problem be solved?

import sys
from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QWidget):
    def __init__(self, parent = None):
        super(MyWindow, self).__init__(parent)
    self.setGeometry(50, 50, 350,350)
    QtGui.QShortcut(QtGui.QKeySequence("Esc"), self, self.close)
        # Create controls
        self.lbl = QtGui.QLabel('Types of Analysis', self)
        self.lbl.setFont(QtGui.QFont('SansSerif', 15) )
        self.cb = QtGui.QComboBox(self)
        self.cb.addItems(['Sentiment Analysis', 'Data Cleansing', 'Genomics', 'Integration', 'Visualization'])
        self.btn = QtGui.QPushButton('Submit', self)

    self.b=QtGui.QLineEdit(self)
    self.b.move(50,130)
    self.cb.currentIndexChanged[str].connect(self.b.setText)

    self.b1=QtGui.QLineEdit(self)
    self.b1.move(50,180)

    # Create layout
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.lbl)
        mainLayout.addWidget(self.cb)
        mainLayout.addWidget(self.b)
        mainLayout.addWidget(self.b1)
        mainLayout.addWidget(self.btn)
        self.setLayout(mainLayout)

    self.btn.clicked.connect(self.printingaction)   

    self.show()


    def printingaction(self):
        print 'Current item: {0}'.format( self.cb.currentIndex() ) # ComboBox's index
        print 'Current index: {0}'.format( self.cb.currentText() ) # ComboBox's text



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = MyWindow()
    sys.exit( app.exec_() )
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Rakshith
  • 77
  • 1
  • 7
  • if it worked, do not forget to mark it as correct, if you do not know how to do it, check the following link: https://stackoverflow.com/tour – eyllanesc Nov 27 '17 at 00:15

1 Answers1

0

To associate a data to an item of the QComboBox you must use the setItemData() method and to obtain the value you use the itemData()

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.setGeometry(50, 50, 350, 350)
        QtGui.QShortcut(QtGui.QKeySequence("Esc"), self, self.close)  # Create controls
        self.lbl = QtGui.QLabel('Types of Analysis', self)
        self.lbl.setFont(QtGui.QFont('SansSerif', 15))
        self.cb = QtGui.QComboBox(self)
        self.btn = QtGui.QPushButton('Submit', self)
        self.b = QtGui.QLineEdit(self)
        self.b1 = QtGui.QLineEdit(self)

        data = [('Sentiment Analysis', 'CS01'),
                ('Data Cleansing', 'CS02'),
                ('Genomics', 'CS03'),
                ('Integration', 'CS04'),
                ('Visualization', 'CS05')]

        self.cb.currentIndexChanged.connect(self.onCurrentIndexChanged)

        for ix, value in enumerate(data):
            subject, code = value
            self.cb.addItem(subject)
            self.cb.setItemData(ix, code, QtCore.Qt.UserRole)

        self.b.setText(self.cb.itemData(0, QtCore.Qt.UserRole).toString())

        # Create layout
        mainLayout = QtGui.QVBoxLayout(self)
        mainLayout.addWidget(self.lbl)
        mainLayout.addWidget(self.cb)
        mainLayout.addWidget(self.b)
        mainLayout.addWidget(self.b1)
        mainLayout.addWidget(self.btn)
        self.setLayout(mainLayout)

        self.btn.clicked.connect(self.printingaction)

    def onCurrentIndexChanged(self, ix):
        combo = self.sender()
        code = combo.itemData(ix, QtCore.Qt.UserRole).toString()
        self.b.setText(code)

    def printingaction(self):
        print('Current item: {0}'.format(self.cb.currentIndex()))  # ComboBox's index
        print('current index: {0}'.format(self.cb.currentText()))  # ComboBox's text


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Hi @eyllanesc,Thanks for the reply.I got this error-self.b.setText(self.cb.itemData(0, QtCore.Qt.UserRole)) TypeError: QLineEdit.setText(QString): argument 1 has unexpected type 'QVariant' – Rakshith Nov 17 '17 at 15:14
  • @Rakshith I already updated it, the error occurred because I thought you were using python3 but you are using python2. – eyllanesc Nov 17 '17 at 15:18