如何在Pyqt中将项添加到动态创建的QCombobox中

时间:2017-12-04 16:58:29

标签: python dynamic override pyqt4 qcombobox

我试图将项目添加到动态创建的组合框中。 我知道我的变量会超越自己,我无法获得理想的结果。 Combobox操作将项目添加到我不想要的最后一个项目。它需要在下一个项目中添加项目。

@Before
public void start(Scenario scenario) throws Exception {
    logger.logTestEnd(String.format("Scenario '%s' start", scenario.getName()));
    ...
}

@After
public void exitScenario(Scenario scenario) throws Exception {
    if (scenario.isFailed()) {
        Screenshot.saveScreenshotInReport(scenario, BrowserManager.getDriver());
        logger.info(String.format("scenario[%s] is failed", scenario.getName()));
    }
    BrowserManager.quit();
    logger.logTestEnd(String.format("Scenario '%s' end", scenario.getName()));
}

我想我需要一些改变。我尝试设置对象名,并以某种方式连接组合框,但我没有。

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
i = 1

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(620, 500)

        #LAYOUT SETTINGS
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))

        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setMinimumSize(QtCore.QSize(30, 30))
        self.pushButton.setMaximumSize(QtCore.QSize(30, 30))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)

        self.pushButton.clicked.connect(self.addNewCombobox)
        self.pushButton.clicked.connect(self.addNewSCombobox)
        self.pushButton.clicked.connect(self.addNewButton)

        self.comboBox = QtGui.QComboBox(self.centralwidget)
        self.comboBox.setMinimumSize(QtCore.QSize(0, 30))
        self.comboBox.setMaximumSize(QtCore.QSize(16777215, 30))
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.gridLayout.addWidget(self.comboBox, 0, 1, 1, 1)

        self.scomboBox = QtGui.QComboBox(self.centralwidget)
        self.scomboBox.setMinimumSize(QtCore.QSize(0, 30))
        self.scomboBox.setMaximumSize(QtCore.QSize(16777215, 30))
        self.scomboBox.setObjectName(_fromUtf8("ScomboBox"))
        self.gridLayout.addWidget(self.scomboBox, 0, 2, 1, 1)

        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem, 9999, 0, 1, 1)
        self.verticalLayout.addLayout(self.gridLayout)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def addNewButton(self):
        print "Added"
        global i
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setMinimumSize(QtCore.QSize(30, 30))
        self.pushButton.setMaximumSize(QtCore.QSize(30, 30))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton.setText("-")
        self.gridLayout.addWidget(self.pushButton, i, 0, 1, 1)
        self.pushButton.clicked.connect(self.comboBox.deleteLater)
        self.pushButton.clicked.connect(self.scomboBox.deleteLater)
        self.pushButton.clicked.connect(self.pushButton.deleteLater)
        i += 1

    def addNewCombobox(self):
        self.comboBox = QtGui.QComboBox(self.centralwidget)
        self.comboBox.setMinimumSize(QtCore.QSize(0, 30))
        self.comboBox.setMaximumSize(QtCore.QSize(16777215, 30))
        self.comboBox.addItem("foo")
        self.comboBox.addItem("bar")
        self.comboBox.setObjectName(_fromUtf8("comboBox_" + str(i)))
        self.gridLayout.addWidget(self.comboBox, i, 1, 1, 1)
        self.comboBox.activated[str].connect(self.addItem)

    def addNewSCombobox(self):
        self.scomboBox = QtGui.QComboBox(self.centralwidget)
        self.scomboBox.setMinimumSize(QtCore.QSize(0, 30))
        self.scomboBox.setMaximumSize(QtCore.QSize(16777215, 30))
        self.scomboBox.setObjectName(_fromUtf8("ScomboBox_" + str(i)))
        self.gridLayout.addWidget(self.scomboBox, i, 2, 1, 1)

    def addItem(self, activetext):
        # sendin = self.centralwidget.sender()
        self.scomboBox.addItem(activetext)


    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.comboBox.setItemText(0, _translate("MainWindow", "New Item", None))
        self.pushButton.setText(_translate("MainWindow", "+", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。找到ObjectName和findChild的使用并进行一些更改.Voila!

这是解决方案。

    def addItem(self, text):
    sendin  = self.centralwidget.sender()
    objname = str(sendin.objectName())
    objnamevalue =  objname.split('_')[1]
    self.centralwidget.findChild(QtGui.QComboBox, 'ScomboBox_' + objnamevalue).addItem(text)