我有一个新的QLineEdit(方框)用于主题代码。每个主题都有一个主题代码(例如,情感分析有代码CS01,数据清理有CS02等)。当我选择一个主题(情绪分析)时主题代码(CS01)应显示在新的编辑框中,当我选择其他主题(数据CLeansing)时,应显示其主题代码(CS02)。问题如何解决?
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_() )
答案 0 :(得分:0)
要将数据与QComboBox
的项目相关联,您必须使用setItemData()
方法并获取使用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_())