Chrisso
Chrisso

Reputation: 11

PYQT Selected Combobox item should remove an item in another combobox

I am very new in Python and just want to build an ui with PYQT. My problems are mainly the mechanism between two comboboxes. In one combobox, the selected item should remove an item in another combobox.

I tried this:

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....

Combobox 1 give an activation signal to combobox 2, maybe i miss the point that the function is not related to the activated signal? The function asked, if the one item is selected in combobox 1, it should remove the item in combobox 2.

To be more clear --> My aim is that at the end is an application where iam able to select an employee in a combobox "xy" who is ill or in vacation and this employee should disappear in another combobox. Iam struggling with this task and iam very frustrated. Maybe, someone has a solution for my problem. :)

Update:

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_())

When i run this code:

It does not matter which item i would select, it always remove "zzz" and "xxx". Even though I try to seperate each item to a function while these functions correspond to self.ui.combobox.currenTextChanged.connection(self.def_1/def_2/def_3) for example. Sorry that i have dramatically missed the point of the example :-/

Upvotes: 1

Views: 8362

Answers (1)

Lester_wu
Lester_wu

Reputation: 171

PyQt has signal/slot mechanism, that is you can do some slot when some signal is being triggered.

For example, in your case, QComboBox has currentTextChanged signal, it will be triggered when you select an item in combobox. When this signal is triggered, you can link it to your slot, and the slot will do the things you want, delete item in another combobox.

Here's an example code :

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

Upvotes: 2

Related Questions