-1

As I mentioned at title, I want to connenct two listiwdgets.(or it is ok to use combobox) What I want to make is that If I click a item of first listwidget, items of second listwidget are changed.

Try) I tried to connect two listwidgets as below

  1. print clicked items
  2. check if printed text is same or not.

Problem) When I click a item in first listwidget, result is not what I want and fail to list-up new items in second listwidget.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):

    def __init__(self):
        self.list1 = ["a","b","c","d"]
        self.list2 = ["alpha","beta","gamma","delta","epsilon","zeta","eta","theta","lota","kappa"]
        self.list3 = ["alpha","beta"]
        self.list4 = ["gamma","delta"]
        self.list5 = ["epsilon","zeta","eta","theta","lota","kappa"]


    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(433, 509)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(70, 450, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.listWidget = QtWidgets.QListWidget(Dialog)
        self.listWidget.setGeometry(QtCore.QRect(20, 10, 256, 192))
        self.listWidget.setObjectName("listWidget")
        self.listWidget.addItems(self.list1)

        self.listWidget_2 = QtWidgets.QListWidget(Dialog)
        self.listWidget_2.setGeometry(QtCore.QRect(20, 230, 256, 192))
        self.listWidget_2.setObjectName("listWidget_2")
        self.listWidget_2.addItems(self.list2)

        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        self.listWidget.itemActivated.connect(self.itemActivated_event)


    def itemActivated_event(self, item):
        print(item.text())
        if print(item.text()) is "a":
            self.listWidget_2.clear()
            self.listWidget_2.addItems(self.list3)

        elif print(item.text()) is "b":
            self.listWidget_2.clear()
            self.listWidget_2.addItems(self.list4)

        else : 
            self.listWidget_2.clear()
            self.listWidget_2.addItems(self.list5)



    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

What I really want is that,

  1. Clicking item "a" in listwidget1, listwidget2 has new item "alpha, beta"
  2. When I click another item "b" in listwidget1, listwidget2 has clear items and has new item "gamma, delta"

There are so many problem because it is my first time to use python and pyqt5.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    I do not understand anything, explain yourself better. What do you want to happen in list2 if you click on item a of the first list? – eyllanesc May 08 '19 at 07:40
  • Click "a" in listwidget1 -> appear "alpha","beta" in listwidget2 Click "b" in listwidget2 -> appear "gamma","delta" in listwidget2 According to item in listwidget1, listwidget2 has different items. That is what I want. – taejin.Park May 08 '19 at 07:55
  • It works for me when I double click on an item. Whether a single or double click is needed to emit `QListQidget.itemActivated` depends on your system. If you want the list to change when single clicking on an item you could connect `self.listWidget.itemClicked` to your slot instead of `self.listWidget.itemActivated`. – Heike May 08 '19 at 07:59
  • I tried again but it doesn't work. After clicking "a" in listwidget1, I click "b" in listwidget1 to change items of listwidget2 but items are not changed. I think it is not matter whether to use itemClicked or itemActivated. – taejin.Park May 08 '19 at 08:15

1 Answers1

0

You have an little mistake:

instead of :

if (item.text()) is "a":

use:

if (item.text()) == "a": 

Difference between == and is operator in Python

The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not.

ncica
  • 7,015
  • 1
  • 15
  • 37