Search code examples
pythonpyqtpyqt5qlineeditqmenu

reading text from QLineEdit as a submenu of a Qmenu option


Reading the text a QlineEdit which is part of a Qmenu

As shown in the code initially I though I could use a trigger function to determine what option changed and then read the text. If I could figure out how to read the QlineEdit with currenttext() from the submenu I could check if the user has changed the input when required.

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QActionGroup, QMenu, QApplication, QLineEdit, QWidgetAction

class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.menubar = self.menuBar()
        self.menubar.installEventFilter(self)
        self.fileMenu = self.menubar.addMenu('&Circuit Set-Up')
        self.populate()

        self.setGeometry(300, 300, 300, 200)
        self.show()


    def triggered(self, action):
        print(action.text())


    def populate(self):

        for m in range(3):

            setattr(self,'impMenu'+str(m),QMenu('Channel'+str(m), self))

            factors=['Enter Transducer Calibration Constant [default 1] = 1',
            'Enter Gauge Factor [default 2] = 2',
            'Passion Ratio [default 0.3] = 0.3']

            for n in range(3):

                ql = QLineEdit(factors[n])
                ql.setMinimumWidth(350)
                wAction = QWidgetAction(self)
                wAction.setDefaultWidget(ql)
                getattr(self,'impMenu'+str(m)).addAction(wAction)

            self.fileMenu.addMenu(getattr(self,'impMenu'+str(m)))
            setattr(self,'triggered'+str(m),self.triggered)
            getattr(self,'impMenu'+str(m)).triggered.connect(getattr(self,'triggered'+str(m)))


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Solution

  • The QLineEdit is above the menu and does not notify you if the user has changed the text so using the information from the QMenu/QAction is unproductive. If you want to get the text when the user changes it then use the QLineEdit signals.

    import sys
    from PyQt5.QtCore import pyqtSlot
    from PyQt5.QtWidgets import QAction, QApplication, QLineEdit, QMainWindow, QMenu, QWidgetAction
    
    
    class Example(QMainWindow):
        def __init__(self):
            super().__init__()
            self.menubar = self.menuBar()
            self.menubar.installEventFilter(self)
            self.fileMenu = self.menubar.addMenu("&Circuit Set-Up")
            self.populate()
    
            self.setGeometry(300, 300, 300, 200)
            self.show()
    
        @pyqtSlot(str)
        def onTextChanged(self, text):
            print(text)
    
        def populate(self):
    
            factors = [
                "Enter Transducer Calibration Constant [default 1] = 1",
                "Enter Gauge Factor [default 2] = 2",
                "Passion Ratio [default 0.3] = 0.3",
            ]
    
            for m in range(3):
                menu = QMenu("Channel{}".format(m), self)
                self.fileMenu.addMenu(menu)
                for n in range(3):
                    ql = QLineEdit(factors[n])
                    ql.setMinimumWidth(350)
                    ql.textChanged.connect(self.onTextChanged)
                    wAction = QWidgetAction(self)
                    wAction.setDefaultWidget(ql)
                    menu.addAction(wAction)
    
    
    if __name__ == "__main__":
    
        app = QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())