Pyqt - 如何更改combobox数据,因为另一个组合框数据? [英] Pyqt - How to change combobox data because of another combobox data?

查看:323
本文介绍了Pyqt - 如何更改combobox数据,因为另一个组合框数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



..我不知道该怎么做...有办法吗? / p>

这里有我的def将combobox放在表中,并打开txt文件到这些组合框中

  def createEd(self,parent,option,index):
if index.column()== POLARITY:
combobox = QComboBox(parent)
combobox.addItems sort(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(\\\
,) )
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(\\\
, ))
combobox.addItems(sorted(lista))
return combobox

elif index.column()== SUBFEATURE:
combobox = QComboBox
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 \\\
,))
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)


解决方案

使用 currentIndexChanged 信号,如下所示:

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

从PyQt4导入QtGui,QtCore

类MyWindow(QtGui.QWidget):
def __init __(self,parent = None):
super(MyWindow, format(x)
对于范围内的x(3)


$ b self.items = dict(zip(
[Parent {0}
],
[
[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)

如果__name__ ==__main__:
import sys

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

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

sys.exit(app.exec_())


I have a table, with 4 columns. Two of this 4 columns are about features. One is Feature, another is subfeature. in each column, there are comboboxes for all cells. I can open txt in these cells. I want to : when i choose cinema for feature, i want to see only name of films in subfeature comboboxes and no every subfeature that i have in my "data"... and when i choose Food in feature, i want to see only types of food in my subfeature comboboxes...

..i dont know how to do it... there is a way to do it?

Here there is my def to put combobox in table and open the txt file into these comboboxes

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)

解决方案

You'll be using the currentIndexChanged signal, something like this:

#!/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_())

这篇关于Pyqt - 如何更改combobox数据,因为另一个组合框数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆