pySide QComboBox刷新

时间:2014-06-23 05:52:27

标签: python combobox pyside

from PySide import QtGui, QtCore

def listItems():
    itemList = ("first","second","etc..")
    return itemList

class tabDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)

        tabWidget = QtGui.QTabWidget()
        tabWidget.addTab(mainTab(), self.tr("Main"))
        tabWidget.addTab(secondTab(), self.tr("Second "))

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(tabWidget)
        self.setLayout(mainLayout)

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

        self.createGroup = QtGui.QGroupBox(self.tr("Add Item to list"))
        self.fileNameEdit = QtGui.QLineEdit(self)        
        self.fileNameEdit.setPlaceholderText("new item")


        self.createButton = QtGui.QPushButton('add',self)
        #self.createButton.clicked.connect(self.additemToList)

        self.createLayout = QtGui.QGridLayout()
        self.createLayout.addWidget(self.fileNameEdit,1,2)
        self.createLayout.addWidget(self.createButton,1,3)
        self.setLayout(self.createLayout) 
        self.createGroup.setLayout(self.createLayout)      


        self.addGroup = QtGui.QGroupBox(self.tr("list items"))
        self.projectLabel = QtGui.QLabel(self.tr("item : "))
        self.projectListCombo = QtGui.QComboBox(self)
        self.projectListCombo.addItems(listItems())
        self.addLayout = QtGui.QHBoxLayout()
        self.addLayout.addWidget(self.projectLabel)
        self.addLayout.addWidget(self.projectListCombo) 
        self.addGroup.setLayout(self.addLayout)

        self.mainLayout = QtGui.QVBoxLayout()
        self.mainLayout.addWidget(self.createGroup)
        self.mainLayout.addWidget(self.addGroup)        
        self.setLayout(self.mainLayout)


class secondTab(QtGui.QWidget):
    def __init__(self,  parent=None):
        QtGui.QWidget.__init__(self, parent)      
        self.initUI()


    def initUI(self):     

        self.addGroup = QtGui.QGroupBox(self.tr("list items"))
        self.projectLabel = QtGui.QLabel(self.tr("item : "))
        self.projectListCombo = QtGui.QComboBox(self)
        self.projectListCombo.addItems(listItems())
        self.addLayout = QtGui.QHBoxLayout()
        self.addLayout.addWidget(self.projectLabel)
        self.addLayout.addWidget(self.projectListCombo) 
        self.addGroup.setLayout(self.addLayout)

        self.mainLayout = QtGui.QVBoxLayout()
        self.mainLayout.addWidget(self.addGroup)        
        self.setLayout(self.mainLayout)



tabdialog = tabDialog()
tabdialog.show()

有2个标签。第一个标签是文本行,按钮添加项目和组合框,第二个标签只是组合框。 两个组合框都包含相同的列表项。 如果将项目添加到列表,则需要刷新两个组合框

1 个答案:

答案 0 :(得分:1)

您可以使用QComboBox::addItem

from PySide import QtGui, QtCore
import sys

def listItems():
    itemList = ("first","second","etc..")
    return itemList


class tabDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)

        self.tabWidget = QtGui.QTabWidget()
        self.mainTab = mainTab(self)
        self.secondTab = secondTab(self)

        self.tabWidget.addTab(self.mainTab, self.tr("Main"))
        self.tabWidget.addTab(self.secondTab, self.tr("Second "))

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.tabWidget)
        self.setLayout(mainLayout)

    def additemToList(self):
        item = self.mainTab.fileNameEdit.text()
        self.mainTab.projectListCombo.addItem(item)
        self.secondTab.projectListCombo.addItem(item)


class mainTab(QtGui.QWidget):
    def __init__(self,  parent=tabDialog):
        QtGui.QWidget.__init__(self, parent)
        self.q = 1234
        self.createGroup = QtGui.QGroupBox(self.tr("Add Item to list"))
        self.fileNameEdit = QtGui.QLineEdit(self)
        self.fileNameEdit.setPlaceholderText("new item")


        self.createButton = QtGui.QPushButton('add',self)
        self.createButton.clicked.connect(parent.additemToList)

        self.createLayout = QtGui.QGridLayout()
        self.createLayout.addWidget(self.fileNameEdit,1,2)
        self.createLayout.addWidget(self.createButton,1,3)
        self.setLayout(self.createLayout)
        self.createGroup.setLayout(self.createLayout)


        self.addGroup = QtGui.QGroupBox(self.tr("list items"))
        self.projectLabel = QtGui.QLabel(self.tr("item : "))
        self.projectListCombo = QtGui.QComboBox(self)
        self.projectListCombo.addItems(listItems())
        self.addLayout = QtGui.QHBoxLayout()
        self.addLayout.addWidget(self.projectLabel)
        self.addLayout.addWidget(self.projectListCombo)
        self.addGroup.setLayout(self.addLayout)

        self.mainLayout = QtGui.QVBoxLayout()
        self.mainLayout.addWidget(self.createGroup)
        self.mainLayout.addWidget(self.addGroup)
        self.setLayout(self.mainLayout)



class secondTab(QtGui.QWidget):
    def __init__(self,  parent=tabDialog):
        QtGui.QWidget.__init__(self, parent)
        self.initUI()


    def initUI(self):

        self.addGroup = QtGui.QGroupBox(self.tr("list items"))
        self.projectLabel = QtGui.QLabel(self.tr("item : "))
        self.projectListCombo = QtGui.QComboBox(self)
        self.projectListCombo.addItems(listItems())
        self.addLayout = QtGui.QHBoxLayout()
        self.addLayout.addWidget(self.projectLabel)
        self.addLayout.addWidget(self.projectListCombo)
        self.addGroup.setLayout(self.addLayout)

        self.mainLayout = QtGui.QVBoxLayout()
        self.mainLayout.addWidget(self.addGroup)
        self.setLayout(self.mainLayout)



def main():
    app = QtGui.QApplication(sys.argv)
    tabdialog = tabDialog()
    tabdialog.show()
    app.exec_()

if __name__ == '__main__':
    main()

我做了以下更改:

  1. mainTabsecondTab中的tabDialog是实例属性
  2. mainTabsecondTab获取tabDialog作为父级
  3. additemToList
  4. 中添加了tabDialog
  5. mainTab.__init__()self.createButton.clicked.connect(parent.additemToList)