PYQT选定的组合框项目应删除另一个组合框中的一个项目

时间:2018-12-18 07:25:01

标签: python-3.x combobox pyqt5

我是Python的新手,只想用PYQT构建ui。我的问题主要是两个组合框之间的机制。在一个组合框中,所选项目应删除另一个组合框中的项目。

我尝试过:

self.ui.combobox1.activated.connect(self.combobox2)  

def remove_Item(self):                            
       if self.ui.combobox1.?????(.currentselection?) == "selected item (name or Index?)": 
             self.ui.combobox2.removeItem(self, Index)
       elif....

组合框1向组合框2提供激活信号,也许我错过了功能与激活信号无关的问题?该函数询问,如果在组合框1中选择了一项,则应删除组合框2中的该项。

更清楚->我的目标是最后一个应用程序,使我能够在组合框“ xy”中选择一个生病或休假的雇员,而该雇员应在另一个组合框中消失。 Iam忙于此任务,Iam非常沮丧。也许有人可以解决我的问题。 :)

更新:

import sys
from qtpy import QtWidgets

from ui.mainwindow import Ui_MainWindow

app = QtWidgets.QApplication(sys.argv)

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent = None):
        super().__init__(parent)

        self.setWindowTitle("ZSP")


        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.B29.addItems(['','xxx', 'yyy', 'zzz'])
        self.ui.comboBox.addItems(['','xxx', 'yyy', 'zzz'])

        self.ui.comboBox.activated.connect(self.example_1)

    def example_1(self):
        index = self.ui.comboBox.findText("xxx")
        self.ui.B29.removeItem(index)
        index_1 = self.ui.comboBox.findText("yyy")
        self.ui.B29.removeItem(index_1)
        index_2 = self.ui.comboBox.findText("zzz")
        self.ui.B29.removeItem(index_2)





window = MainWindow()

window.show()

sys.exit(app.exec_())

当我运行此代码时:

我选择哪个项目都没有关系,它总是删除“ zzz”和“ xxx”。即使我尝试将每个项目分离为一个函数,而这些函数对应于例如self.ui.combobox.currenTextChanged.connection(self.def_1 / def_2 / def_3)。抱歉,我大大错过了示例的要点:-/

1 个答案:

答案 0 :(得分:0)

PyQt具有信号/时隙机制,即当某些信号被触发时,您可以做一些时隙。

例如,在您的情况下,QComboBox具有 currentTextChanged 信号,当您在组合框中选择一个项目时将触发该信号。触发此信号后,您可以将其链接到插槽,插槽将完成您想要的事情,删除另一个组合框中的项目。

这是示例代码:

class MainWindow(QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.combo1 = QComboBox()
        self.combo2 = QComboBox()
        self.combo1.addItems(['1', '2', '3', '4', '5'])
        self.combo2.addItems(['1', '2', '3', '4', '5'])
        # This line link the signal - currentTextChanged to slot - del_item_in_combo
        self.combo1.currentTextChanged.connect(self.del_item_in_combo)
        layout = QHBoxLayout()
        layout.addWidget(self.combo1)
        layout.addWidget(self.combo2)
        self.setLayout(layout)
        self.setWindowTitle("combo box demo")

    def del_item_in_combo(self, text):
        # currentTextChanged signal will pass selected text to slot
        index = self.combo2.findText(text)  # find the index of text
        self.combo2.removeItem(index)  # remove item from index