Skip to content
Advertisement

Returning value from ListWidget Selection in another class – PyQt5

I’m working on a script where when a user clicks on a button, a pop-up window will appear that contains a list. When the user doubleclicks on an item from that list, a label will populate on the original window with the selection. I’ve got most of the code working, but I’m having trouble on returning the selected value from the list back to the original window. Here is the code I’ve made so far:

”’

class Main(QWidget):

    def __init__(self):
        super(Main, self).__init__()

        self.fgl = [] # Contains a list, left blank in the example
        self.tag = None

        self.BA = QPushButton("Phase A: ", self)
        self.BA.resize(60,25)
        self.BA.move(10,25)

        self.LA = QLabel("",self)
        self.LA.resize(100,25)
        self.LA.move(75,25)
        self.LA.setStyleSheet("border: 1px solid black;")

        self.BA.clicked.connect(self.tpuA)

    def tpuA(self):
        self.tag = Tag_PopUp()
        self.tag.build(self.fgl)
        self.tag.setGeometry(QRect(500, 300, 325, 200))
        self.tag.show()

    def updateText(self, item):
        self.LA.setText(item)


class Tag_PopUp(QWidget):

    def __init__(self):
        super(Tag_PopUp,self).__init__()
        self.listW = QListWidget(self)
        self.listW.itemDoubleClicked.connect(self.updateItem)

    def build(self, fgl):
        for i in fgl:
            QListWidgetItem(i, self.listW)

    def updateItem(self, item):
        x = item.text()
        #PopUp.updateText(x)
        return x

”’

Any suggestions on where I am going wrong or what the last lines of code I need are? Let me know if you need additional information in order to help me out

Advertisement

Answer

Create a signal on your Tag_PopUp that emits when the user clicks the item in the list, and connect it to your Main Widget to update the label. I made notes in the code below where I made changes.

For Example:

class Main(QWidget):

    def __init__(self):
        super(Main, self).__init__()

        self.fgl = [] # Contains a list, left blank in the example
        self.tag = None

        self.BA = QPushButton("Phase A: ", self)
        self.BA.resize(60,25)
        self.BA.move(10,25)

        self.LA = QLabel("",self)
        self.LA.resize(100,25)
        self.LA.move(75,25)
        self.LA.setStyleSheet("border: 1px solid black;")

        self.BA.clicked.connect(self.tpuA)

    def tpuA(self):
        self.tag = Tag_PopUp()
        self.tag.build(self.fgl)
        self.tag.setGeometry(QRect(500, 300, 325, 200))
        self.tag.itemUpdated.connect(self.updateText)  # added this
        self.tag.show()

    def updateText(self, item):
        self.LA.setText(item)


class Tag_PopUp(QWidget):

    itemUpdated = pyqtSignal([str])  # added this

    def __init__(self):
        super(Tag_PopUp,self).__init__()
        self.listW = QListWidget(self)
        self.listW.itemDoubleClicked.connect(self.updateItem)

    def build(self, fgl):
        for i in fgl:
            QListWidgetItem(i, self.listW)

    def updateItem(self, item):
        x = item.text()
        self.itemUpdated.emit(x)  # added this
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement