Pyqt-如何因另一个组合框数据而更改组合框数据?

2024-05-17

我有一个表,有 4 列。 这 4 列中的两列是关于功能的。一个是特征,另一个是子特征。 在每一列中,所有单元格都有组合框。 我可以在这些单元格中打开txt。 我想:当我选择电影院作为功能时,我只想看到子功能组合框中的电影名称,而不是我的“数据”中的每个子功能......当我选择功能中的食物时,我只想看到我的子功能组合框中的食物类型...

..我不知道该怎么做...有办法做到吗?

这是我将组合框放入表中并将文本文件打开到这些组合框中的定义:

def createEd(self, parent, option, index):
    if index.column() == POLARITY: 
        combobox = QComboBox(parent)
        combobox.addItems(sorted(index.model().TPolarities))
        combobox.setEditable(True)
        arquivo = codecs.open("ln2.txt",encoding='utf-8',mode="r")   
        conTWordsdo = arquivo.readlines()
        lista =[]        
        for i in conTWordsdo:
            lista.append(i.replace("\n",""))
        combobox.addItems(sorted(lista))
        return combobox            
    elif index.column() == FEATURE: 
        combobox = QComboBox(parent)
        combobox.addItems(sorted(index.model().TFeatures))
        combobox.setEditable(True)
        arquivo = codecs.open("ln1.txt",encoding='utf-8',mode="r")  
        conTWordsdo = arquivo.readlines()
        lista = []            
        for i in conTWordsdo:
            lista.append(i.replace("\n",""))
        combobox.addItems(sorted(lista))
        return combobox

    elif index.column() == SUBFEATURE: 
        combobox = QComboBox(parent)
        combobox.addItems(sorted(index.model().TSubFeatures))
        combobox.setEditable(True)
        arquivo = codecs.open("ln3.txt",encoding='utf-8',mode="r")  
        conTWordsdo = arquivo.readlines()
        lista = []            
        for i in conTWordsdo:
            lista.append(i.replace("\n",""))
        combobox.addItems(sorted(lista))
        return combobox             

    elif index.column() == SENTENCE:
        editor = QLineEdit(parent)
        self.connect(editor, SIGNAL("returnPressed()"), self.commitAndCloseEditor)
        return editor
    else:
        return QItemDelegate.createEditor(self, parent, option, index)

您将使用当前索引已更改 http://qt-project.org/doc/qt-4.8/qcombobox.html#currentIndexChanged-2信号,像这样:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtGui, QtCore

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

        self.items = dict(zip(
            [   "Parent {0}".format(x)
                for x in range(3)
                ],
            [   
                [ "Child {0} - {1}".format(x, y)
                    for y in range(3)
                    ]
                for x in range(3)
                ]
        ))

        self.comboBoxChild = QtGui.QComboBox(self)

        self.comboBoxParent = QtGui.QComboBox(self)
        self.comboBoxParent.addItems(self.items.keys())
        self.comboBoxParent.currentIndexChanged[str].connect(self.on_comboBoxParent_currentIndexChanged)
        self.comboBoxParent.setCurrentIndex(1)

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.comboBoxParent)
        self.layoutVertical.addWidget(self.comboBoxChild)

    @QtCore.pyqtSlot(str)
    def on_comboBoxParent_currentIndexChanged(self, index):
        items = self.items[str(index)]

        self.comboBoxChild.clear()
        self.comboBoxChild.addItems(items)

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()
    main.resize(222, 111)

    sys.exit(app.exec_())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pyqt-如何因另一个组合框数据而更改组合框数据? 的相关文章

随机推荐